home *** CD-ROM | disk | FTP | other *** search
Prolog Source | 1990-03-26 | 858 b | 29 lines |
- /*
- Copyright (c) 1986, 90 by Prolog Development Center
- */
-
- domains
- list = integer* /* or whatever type you want to use */
-
- predicates
- length_of(list, integer, integer)
-
- clauses
- /* * * * * * * * * * * * * * * * * * * * * * *
- * If the list is empty, bind the second arg *
- * (the result) to the third (the counter).*
- * * * * * * * * * * * * * * * * * * * * * * */
- length_of([], Result, Result).
-
- /* * * * * * * * * * * * * * * * * * * * * * * * * * *
- * Otherwise, add 1 to counter and recurse, binding *
- * Result in this invocation to Result in the next. *
- * * * * * * * * * * * * * * * * * * * * * * * * * * */
- length_of([_|T], Result, Counter) :-
- NewCounter = Counter + 1,
- length_of(T, Result, NewCounter).
-
- goal
- length_of([1, 2, 3], L, 0), /* start with Counter = 0 */
- write(L), nl.