Maple Professional
Maple Academic
Maple Student Edition
Maple Personal Edition
Maple Player
Maple Player for iPad
MapleSim Professional
MapleSim Academic
Maple T.A. - Testing & Assessment
Maple T.A. MAA Placement Test Suite
Möbius - Online Courseware
Machine Design / Industrial Automation
Aerospace
Vehicle Engineering
Robotics
Power Industries
System Simulation and Analysis
Model development for HIL
Plant Modeling for Control Design
Robotics/Motion Control/Mechatronics
Other Application Areas
Mathematics Education
Engineering Education
High Schools & Two-Year Colleges
Testing & Assessment
Students
Financial Modeling
Operations Research
High Performance Computing
Physics
Live Webinars
Recorded Webinars
Upcoming Events
MaplePrimes
Maplesoft Blog
Maplesoft Membership
Maple Ambassador Program
MapleCloud
Technical Whitepapers
E-Mail Newsletters
Maple Books
Math Matters
Application Center
MapleSim Model Gallery
User Case Studies
Exploring Engineering Fundamentals
Teaching Concepts with Maple
Maplesoft Welcome Center
Teacher Resource Center
Student Help Center
Fold Operators - compose a binary operator
Calling Sequence
foldl(rator, id, r1,r2, ...)
foldr(rator, id, r1, r2, ...)
Parameters
rator
-
operator to apply
id
identity or initial value
r1, r2, ...
(optional) operands for the call
Description
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, .
The right-fold operator foldr is similar but associates operands from the right. For example, .
More formally, the left-fold operator foldl is defined recursively by and for a positive integer n.
Similarly, the right-fold operator foldr is defined recursively by and 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.
Examples
Define a dot product as follows.
Although it is not implemented this way, the left-fold operator can be written as one line in Maple.
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.
Compute horner forms using foldl.
Count the number of elements of a list.
Reverse a list. Note that this is not the fastest method.
By generating lists, you can compute more than one quantity at a time.
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:
See Also
andmap, function, map, operator, ormap, procedure, seq, zip
References
Hutton, Graham. "A tutorial on the universality and expressiveness of fold." J. Functional Programming, Vol. 1(1). (January 1993): 1-17.
Download Help Document