Church numerals: Difference between revisions

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.)
 
<pre>
(n g v) ; uncurried, 1 namespace
((n g) v) ; curried, 1 namespace
(funcall n g v) ; uncurried, 2 namespaces
(funcall (funcall n g) v) ; curried, 2 namespaces
</pre>
 
===Uncurried Church numerals===
Line 707 ⟶ 701:
{{trans|Acornsoft Lisp}}
 
In this section, Church numerals are uncurried, 2-argument functions. of the form
<pre>(lambda (f x) ...)</pre>
 
<syntaxhighlight lang="lisp">
(defvar zero (lambda (f x) x))
 
(defvar one (lambda (f x) (funcall f 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
(defvar one <pre>(lambda (f) (lambda (x) (funcall f x))...))</pre>
 
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 in uncurriedthe version abovethat uses uncurried numerals.
 
===Further Church numerals===
Line 809 ⟶ 806:
(defvar true (lambda (f g) (funcall f)))
(defvar false (lambda (f g) (funcall g)))
 
(defun bool (v) (if v true false))
 
(defun unbool (b)
(church-if b t nil))
</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 above.
 
<syntaxhighlight lang="lisp">
Anonymous user