Don't ask me Y you need this (the Y combinator in Nu)

Sunday, 13 Jan 2008

Following John Franco, here is a definition of the Y combinator in Nu:

(function Y (X)
     ((do (procedure)
          (X (do (arg) ((procedure procedure) arg))))
      (do (procedure)
          (X (do (arg) ((procedure procedure) arg))))))
Now let’s use it to create a recursive factorial function:
(function F* (func-arg)
     (do (n)
         (if (eq 0 n)
             (then 1)
             (else (* n (func-arg (- n 1)))))))

(set fact (Y F*))

And be sure to test your new function; otherwise you’ll never believe me:

% (fact 5)
120
Now, do you really need to know this? Probably not. In Nu, you could create that same function in this much simpler manner (which I recommend!):
(function fact (n)
     (if (eq 0 n)
         (then 1)
         (else (* n (fact (- n 1))))))

But this might give you something interesting to discuss at parties.

Comments (0) post a reply