Counting Sublist Elements in Prolog
Posted
by idea_
on Stack Overflow
See other posts from Stack Overflow
or by idea_
Published on 2009-09-06T05:38:49Z
Indexed on
2010/05/22
23:30 UTC
Read the original article
Hit count: 246
How can I count nested list elements in prolog?
I have the following predicates defined, which will count a nested list as one element:
length([ ], 0).
length([H|T],N) :- length(T,M), N is M+1.
Usage:
?- length([a,b,c],Out).
Out = 3
This works, but I would like to count nested elements as well i.e.
length([a,b,[c,d,e],f],Output).
?- length([a,b,[c,d,e],f],Output).
Output = 6
© Stack Overflow or respective owner