List Length in Prolog -
i beginner in prolog programming. wrote program calculate length of list. why below program wrong?
length(0, []). length(l+l, h|t) :- length(l, t). i wrote below program , works correctly.
length([], 0). length([h|t], n) :- length(t, n1), n n1+1. when changed order, got error. why?
length([], 0). length([h|t], n) :- n n1+1, length(t, n1).
this code
length_1(0,[]). length_1(l+1, [h|t]) :- length_1(l,t). works (please note added square braces), in unexpected way. build expression tree, evaluated later:
?- length_1(x, [1,2,3]), l x. x = 0+1+1+1, l = 3. in rewritten code (second version), error because n1 not yet instantiated @ time call is/2.
Comments
Post a Comment