Did you know ... | Search Documentation: |
Predicate scanl/6 |
<=
m <=
4) lists of length n head-to-tail ("scan-left"), using columns of m
list elements as arguments for Goal. The scanl
family of predicates is defined as follows, with V0 an
initial value and V the final value of the scanning
operation:
scanl(G, [X_11, ..., X_1n], [X_21, ..., X_2n], ..., [X_m1, ..., X_mn], V0, [V0, V1, ..., Vn] ) :- call(G, X_11, ..., X_m1, V0, V1), call(G, X_12, ..., X_m2, V1, V2), ... call(G, X_1n, ..., X_mn, V<n-1>, Vn).
scanl
behaves like a foldl
that collects
the sequence of values taken on by the Vx accumulator into a
list.
?- scanl(plus, [1,2,3,4,5], 0, Sums). Sums = [0, 1, 3, 6, 10, 15].
When considering the lists columns of a table, scanl/5 combines the accumulator with each row to produce the next value in the result list. We illustrate this using plus/4 which adds the accumulator with the values from the row to produce the next accumulator. We use trace/2 to illustrate how the values are combined.
plus(A,B,C,Result) :- Result is A+B+C.
?- trace(plus/4, exit). % plus/4: [exit] ?- scanl(plus, [1,2,3,4,5], [10,20,30,40,50], 0, Sums). T Exit: plus(1, 10, 0, 11) T Exit: plus(2, 20, 11, 33) T Exit: plus(3, 30, 33, 66) T Exit: plus(4, 40, 66, 110) T Exit: plus(5, 50, 110, 165) Sums = [0, 11, 33, 66, 110, 165].