3 ms·
No. This is the standard implementation of the length/2 predicate. Like any other language, prolog has a standard library that includes this predicate. You'd ne
by tathougies 10y ago
No. This is the standard implementation of the length/2 predicate. Like any other language, prolog has a standard library that includes this predicate. You'd never need to write this in practice, but this is how it would be written in the standard library.
In general, any general purpose language worth it's salt will have substantial portions of its standard library written in that language, and in this regard, prolog meets the criteria for respectsbl languages
- Koshkin 10y agoI guess, the point was, if list is a built-in construct, the function length should be also built in. (Unless, that is, natural numbers are defined in the standard library.)
- YeGoblynQueenne 10y agoProlog lists are predicates, like pretty much everything else in the language. Lists have the functor '.' (the dot) and two arguments, a term and a list, so they're defined recursively. Frex, this is a list: .(a, .(b, .(c, .(d, [])))) '[]' is an atom that stands for the empty list. Normally however we write lists like this: [a,b,c,d] Which is syntactic sugar used in the vast majority of Prolog code. The easiest way to think of it is that a list is a pattern, consisting of two square brackets enclosing any number of terms. You can't really call functions on it, but you can pass it as an argument to other predicates, and match it to other patterns.
- tom_mellior 10y ago> I guess, the point was, if list is a built-in construct, the function length should be also built in. It is built in. No need to write your own length predicate. The quoted example is an illustration of a possible implementation. (Cue boring discussion of "built in" vs. "defined in the standard library".)
- vidarh 10y agoThat something is "built in" doesn't mean it won't be defined somewhere, such as in the standard library.