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:
|