Prolog Language Higher-Order Programming foldl/4


A fold (from the left) is a higher-order relation between:

  • a predicate with 3 arguments
  • a list of elements
  • an initial state
  • a final state, which is the result of applying the predicate to successive elements while carrying through intermediate states.

For example: Use foldl/4 to express the sum of all elements in a list, using a predicate as a building block to define the sum of two elements:

?- foldl(plus, [2,3,4], 0, S).
S = 9.