Scan Higher Order Function
In functional programming terms, the prefix sum may be generalized to any binary operation (not just the addition operation); the higher order function resulting from this generalization is called a scan, and it is closely related to the fold operation. Both the scan and the fold operations apply the given binary operation to the same sequence of values, but differ in that the scan returns the whole sequence of results from the binary operation, whereas the fold returns only the final result. For instance, the sequence of factorial numbers may be generated by a scan of the natural numbers using multiplication instead of addition:
-
input numbers 1 2 3 4 5 6 ... prefix products 1 2 6 24 120 720 ...
In Haskell, there are two variants of scan, called scanl
and scanl1
, differing slightly in their argument signature, and the prefix sum operation may be written scanl1 (+)
. The corresponding suffix operations are also available as scanr
and scanr1
. The procedural Message Passing Interface libraries provide an operation MPI_Scan
for computing a scan operation between networked processing units. The C++ language has a standard library function partial_sum
; despite its name, it takes a binary operation as one of its arguments, and performs a scan with that operation.
Read more about this topic: Prefix Sum
Famous quotes containing the words higher, order and/or function:
“Science, unguided by a higher abstract principle, freely hands over its secrets to a vastly developed and commercially inspired technology, and the latter, even less restrained by a supreme culture saving principle, with the means of science creates all the instruments of power demanded from it by the organization of Might.”
—Johan Huizinga (18721945)
“It is the international system of currency which determines the totality of life on this planet. That is the natural order of things today. That is the atomic, and sub-atomic, and galactic
structure of things today. And you have meddled with the primal forces of nature! And you will atone! Am I getting through to you, Mr. Beale?”
—Paddy Chayefsky (19231981)
“Of all the inhabitants of the inferno, none but Lucifer knows that hell is hell, and the secret function of purgatory is to make of heaven an effective reality.”
—Arnold Bennett (18671931)