Anonymous user
Church numerals: Difference between revisions
→{{header|Common Lisp}}
imported>Rowsety Moid |
imported>Rowsety Moid |
||
Line 694:
Common Lisp has separate namespaces for variable and function names. When a variable, <code>f</code>, has a function as its value, the usual function-calling syntax, <code>(f ...)</code> can't be used to call the function; instead you have to write
<pre>(funcall f ...)</pre>
(<code>Funcall</code> is also needed when the function is the value of a more complex expression, rather than just a variable.)
===Uncurried Church numerals===
Line 707 ⟶ 701:
{{trans|Acornsoft Lisp}}
In this section, Church numerals are uncurried, 2-argument functions
<pre>(lambda (f x) ...)</pre>
<syntaxhighlight lang="lisp">
(defvar zero (lambda (f x) x))
(defun succ (n) (lambda (f x) (funcall f (funcall n f x))))
Line 735 ⟶ 728:
example (unchurch (eval example))))
(defvar one (succ zero))
(defvar two (succ one))
(defvar three (succ two))
Line 759 ⟶ 753:
===Curried Church numerals===
Here church numerals are curried functions of the form
However, other functions are not curried. <code>Plus</code>, for example, is an ordinary, 2-argument function.
<syntaxhighlight lang="lisp">
(defvar zero (lambda (f) (lambda (x) x)))
▲(defvar one (lambda (f) (lambda (x) (funcall f x))))
(defun succ (n) (lambda (f) (compose f (funcall n f))))
Line 786 ⟶ 783:
</syntaxhighlight>
The remaining definitions, the calls to <code>show</code>, and the resulting output are the same as
===Further Church numerals===
Line 809 ⟶ 806:
(defvar true (lambda (f g) (funcall f)))
(defvar false (lambda (f g) (funcall g)))
</syntaxhighlight>
Line 846 ⟶ 838:
===Further, curried===
Again, the remaining definitions, the calls to <code>show</code>, and the resulting output are the same as in uncurried version
<syntaxhighlight lang="lisp">
|