[net.lang] {Re:}* Recursion

jhf (04/20/83)

Yes indeed, probably the most natural way to define the factorial function
is as the product of the integers 1 through n.  I think one sees the
the version

	fact(n) = if n=0 then 1 else n*fact(n-1)

so often not because it is the best way to define factorial, but because
it is one of the simplest functions by which to demonstrate recursion.

Take a look David Turner's (U. of Kent at Canterbury) language KRC.
In it, one can write (I may not have the syntax exactly right.)

	fact n = product [1..n]

after defining

	product L = reduce times 1 L

where

	reduce f ident nil = ident
	reduce f ident x:L = f x (reduce f ident L)

The ":" denotes the pairing operation, like Lisp cons.  The  ".." notation
is a shorthand for constructing lists (possibly infinite) of consecutive
integers.  Turner says it has proved useful for eliminating the sort
of "junk recursion" we see in the first implementation of fact, above.

I have probably mucked up this example, although I think I have the spirit
of it right.  Any comments from folks at ukc?

Joe Fasel
Los Alamos National Laboratory
{ucbvax!lbl-csam | pur-ee!purdue | harpo!floyd!cmcl2}!lanl-a!jhf
JFasel@LANL

billw@sri-unix.UUCP (06/06/83)

#R:uvacs:-59700:sri-unix:13600005:000:52
sri-unix!billw    Apr 21 18:12:00 1983

How about APLs:

	fact _ */[IOTA] x

or whatever...