next up previous
Next: 8 Summary Up: Recursion, Iteration and Functional Previous: 6.2 Tail-Recursive Fibonacci

7 Iteration and Recursion Operators

Functional languages often have functional abstractions for iteration and recursion. For example, Scheme has a mapping (iteration abstraction) function named map which applies a function to each item of a list.

(map (lambda(x) (* x x x)) '(2 3 4 5))

produces the result

(8 27 64 125)

This same example is easily expressed using J. In J, the power function, expressed as ^, may be used as the argument of the bond conjunction, &, producing the cube function ^&3. This monad may be applied as:

(^&3) 2 3 4 5

producing the result

8 27 64 125

The J programming notation has a rich collection of abstractions for dealing with iteration over the items in lists and arrays. For example, the insert adverb, /, allows a dyad to be inserted between the items of a list or array.

+/ 1 2 3 4 5

evaluates the expression

1 + 2 + 3 + 4 + 5

while

*/ 1 2 3 4 5 6

evaluates the expression

1 * 2 * 3 * 4 * 5 * 6

Scheme and J both support an extensive family of abstractions for recursion and iteration which are beyond the scope of this paper. Such features are important in the exposition of a more advanced treatment of recursion and iteration. For example, Iverson [Iv 95] shows extensive use of J to construct mathematical proofs of correctness of algorithms.


next up previous
Next: 8 Summary Up: Recursion, Iteration and Functional Previous: 6.2 Tail-Recursive Fibonacci
2002-11-26