I am trying to implement foldl1 in prolog without using build-in foldl predicate.
My code produce Syntax error: Operator expected
:
foldl1(_, [E], E).
foldl1(Predicate, [X,Y|Z], Result) :-
call(Predicate(X), Y, Ans),
foldl1(Predicate, [Ans|Z], Result).
I expect:
?- foldl1(concat,['a','b','c','d'],X).
X = abcd.
Thank you!
Predicate(X)
is indeed invalid syntax as (in standard Prolog), the name of a compound term cannot be a variable. Use instead:
foldl1(_, [E], E).
foldl1(Predicate, [X,Y|Z], Result) :-
call(Predicate, X, Y, Ans),
foldl1(Predicate, [Ans|Z], Result).