Fold Operators
compose a binary operator
Calling Sequence
Parameters
Description
Examples
References
foldl(rator, id, r1,r2, ...)
foldr(rator, id, r1, r2, ...)
rator
-
operator to apply
id
identity or initial value
r1, r2, ...
(optional) operands for the call
The left-fold operator foldl composes a binary operator rator, with identity or initial value id onto its arguments r1,r2,... (which may be zero in number), associating from the left. For example, given three arguments a, b, and c, and initial value id, foldl⁡rator,id,a,b,c=rator⁡rator⁡rator⁡id,a,b,c.
The right-fold operator foldr is similar but associates operands from the right. For example, foldr⁡rator,id,a,b,c=rator⁡a,rator⁡b,rator⁡c,id.
More formally, the left-fold operator foldl is defined recursively by foldl⁡rator,id=id and foldlrator,id,e1,e2,⁢...,⁢en=foldlrator,ratorid,e1,⁢e2,⁢...,en for a positive integer n.
Similarly, the right-fold operator foldr is defined recursively by foldr⁡rator,id=id and foldrrator,id,e1,e2…,en=ratore1,foldrrator,id,e2,…,en for a positive integer n.
When the operator rator being folded is an associative binary operator, both the left-fold and right-fold operators produce the same result. However, foldl is more efficient than foldr.
The fold operators are useful for implementing operations of variable arity in terms of binary operations. See the and example in the Examples section.
foldl⁡F,id,a,b,c,d
F⁡F⁡F⁡F⁡id,a,b,c,d
foldr⁡F,id,a,b,c,d
F⁡a,F⁡b,F⁡c,F⁡d,id
foldl⁡`+`,0,a,b,c
a+b+c
foldl⁡`and`,true,a,b,c
aandbandc
Define a dot product as follows.
dot≔u,v↦foldl⁡`+`,0,op⁡zip⁡`*`,u,v:
dot⁡1,2,3,−1,−2,−3
−14
define⁡aF,associative:
foldl⁡aF,NULL,a,b,c
aF⁡a,b,c
foldr⁡aF,NULL,a,b,c
aF⁡b,c,a
Although it is not implemented this way, the left-fold operator can be written as one line in Maple.
myfoldl≔rator,id↦`@`⁡seq⁡t↦u↦rator⁡u,t⁡args1+nargs−i,i=1..nargs−2⁡id:
myfoldl⁡F,id,a,b,c,d
A typical use of the fold operators is in the implementation of n-ary variants of binary operators. The and operator in Maple accepts only two arguments. A version that accepts any number of arguments can be implemented by using a fold operator as follows. Note that it does not matter which one is used, given that and is associative.
andn≔↦foldl⁡`and`,true,args
andn≔↦foldl⁡and,true,args
andn⁡
true
andn⁡a
a
andn⁡a,b
aandb
andn⁡a,b,c,d,e
aandbandcanddande
Compute horner forms using foldl.
p≔sort⁡randpoly⁡x
p≔−7⁢x5+22⁢x4−55⁢x3−94⁢x2+87⁢x−56
foldl⁡a,b↦a⋅x+b,op⁡map2⁡op,1,op⁡p
−7⁢x+22⁢x−55⁢x−94⁢x+87⁢x−56
Count the number of elements of a list.
count≔L↦foldr⁡x,n↦n+1,0,op⁡L
count⁡a,b,c,d,e
5
Reverse a list. Note that this is not the fastest method.
lreverse≔L↦foldl⁡u,v↦v,op⁡u,,op⁡L
lreverse⁡1,2,3,4,5
5,4,3,2,1
By generating lists, you can compute more than one quantity at a time.
SumLen≔L↦foldr⁡n,u↦n+u1,1+u2,0,0,op⁡L
SumLen⁡1,2,3,4,5
15,5
The following example demonstrates an efficient way to test whether a particular predicate returns true for some member of a set or list. The example uses careful sequencing of automatic simplifications and Maple's evaluation rules (which include McCarthy rules for the evaluation of the Boolean operators and and or) to ensure that the procedure returns as early as possible.
my_ormap := proc( pred::procedure, L::{list,set} ) option inline; eval(foldl( '`or`', false, op( map( ''pred'', L ) ) )) end proc:
p := proc(n) print( n ); isprime( n ) end proc:
my_ormap⁡p,seq⁡i,i=1..10
1
2
Hutton, Graham. "A tutorial on the universality and expressiveness of fold." J. Functional Programming, Vol. 1(1). (January 1993): 1-17.
See Also
andmap
function
map
operator
ormap
procedure
seq
zip
Download Help Document