On this page:
Y
Y*
fnlet
partial
compose
identity

3.11 Theory

procedure

(Y fn)  fn?

  fn : fn?
The strict Y fixed-point combinator. Allows for recursion of anonymous functions. Given a fn1 which contains a single named argument, and within which is an additional single-argument fn2, the innermost fn2 can call the named argument of fn1 as if it were a function name in order to recur on itself. For example, the factorial function can be defined thusly, using the Y-combinator:

Example:
> (def Fact
    (Y
     (fn (fact)
       (fn (n)
         (if (zero? n)
             then 1
             else (* n (fact (- n 1))))))))

Note however that usage of the Y-combinator for recursion is not especially efficient, and the more traditional recursive approach is generally recommended whenever possible (which is most of the time).

procedure

(Y* fn)  fn?

  fn : fn?
A generalization of the Y-combinator that allows the function to take any number of arguments.

syntax

(fnlet name args body ...+)

Equivalent to (Y* (fn (name) (fn args body ...))). For example, to map the Fibonacci sequence without defining a named function to do it:

Example:
> (map (fnlet fib (n)
         (select
          ((zero? n) 0)
          ((one? n) 1)
          (else (+ (fib (- n 2)) (fib (- n 1))))))
       (range 0 to 20))

'(0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765)

procedure

(partial fun arg ...)  fn?

  fun : fn?
  arg : any
Returns a function with the args partially applied to fun, which can then be passed the remaining arguments, as many as needed to complete the calculation. For example:

Example:
> (map (partial + 2) (range 1 to 4))

'(3 4 5 6)

procedure

(compose fn1 fn2)  fn?

  fn1 : fn?
  fn2 : fn?
Returns a new function which is a composition of fn1 and fn2. This function evaluates fn2 with its arguments, and then applies fn1 to the result of fn2.

Examples:
> (def abs-sub (compose abs -))
> (abs-sub 4 5)

1

procedure

(identity v)  any?

  v : any?
Returns v.