List Length in Prolog

You need to use an accumulator. While you could do something like this: which will recurse all the way down to the end of the list and then, as each invocation returns, add one to the length, until it gets back to the top level with the correct result. The problem with this approach is that … Read more

Appending a list in a loop (R)

There are four errors in your approach. First, file.names <- dir(path, pattern =”.csv”) will extract just file names, without path. So, when you try to import then, read.csv() doesn’t find. Building the path You can build the right path including paste0(): Or file.path(), which add slashes automaticaly. And another way to create the path, for … Read more

How do I append lists in Prolog?

The code as you’ve posted it is (almost) OK. The order of clauses just needs to be swapped (in order to make this predicate definition productive, when used in a generative fashion): This defines a relationship between the three arguments, let’s say A, B and C. Your first line says, ” C is the result of appending A and B if A and C are non-empty lists, they both have … Read more

Append value to empty vector in R?

Appending to an object in a for loop causes the entire object to be copied on every iteration, which causes a lot of people to say “R is slow”, or “R loops should be avoided”. As BrodieG mentioned in the comments: it is much better to pre-allocate a vector of the desired length, then set the element … Read more