Maple Professionel
Maple Académique
Maple Edition Étudiant
Maple Personal Edition
Maple Player
Maple Player for iPad
MapleSim Professionel
MapleSim Académique
Maple T.A. - Suite d'examens de classement
Maple T.A. MAA Placement Test Suite
Möbius - Didacticiels de mathématiques en ligne
Machine Design / Industrial Automation
Aéronautique
Ingénierie des véhicules
Robotics
Energie
System Simulation and Analysis
Model development for HIL
Modélisation du procédé pour la conception de systèmes de contrôle
Robotics/Motion Control/Mechatronics
Other Application Areas
Enseignement des mathématiques
Enseignement de l’ingénierie
Enseignement secondaire et supérieur (CPGE, BTS)
Tests et évaluations
Etudiants
Modélisation financière
Recherche opérationnelle
Calcul haute performance
Physique
Webinaires en direct
Webinaires enregistrés
Agenda des évènements
Forum MaplePrimes
Blog Maplesoft
Membres Maplesoft
Maple Ambassador Program
MapleCloud
Livres blancs techniques
Bulletin électronique
Livres Maple
Math Matters
Portail des applications
Galerie de modèles MapleSim
Cas d'Etudes Utilisateur
Exploring Engineering Fundamentals
Concepts d’enseignement avec Maple
Centre d’accueil utilisateur Maplesoft
Centre de ressources pour enseignants
Centre d’assistance aux étudiants
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