Church numerals: Difference between revisions

Content added Content deleted
imported>Rowsety Moid
No edit summary
imported>Rowsety Moid
Line 683: Line 683:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==

In many solutions to this task, Church numerals are given a [[Currying|Curried]] form. In Common Lisp, that looks like this:

<pre>(lambda (f) (lambda (x) ...))</pre>

However, they can also be given an uncurried, 2-argument form:

<pre>(lambda (f x) ...)</pre>

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===
===Uncurried Church numerals===


{{trans|Acornsoft Lisp}}
{{trans|Acornsoft Lisp}}

In this section, Church numerals are uncurried, 2-argument functions.


<syntaxhighlight lang="lisp">
<syntaxhighlight lang="lisp">
Line 704: Line 725:
(funcall n (lambda (product) (times m product)) one))
(funcall n (lambda (product) (times m product)) one))


(defun church (i)
(defun church (i) ; int -> Church
(if (zerop i) zero (succ (church (1- i)))))
(if (zerop i) zero (succ (church (1- i)))))


(defun unchurch (n)
(defun unchurch (n) ; Church -> int
(funcall n #'1+ 0))
(funcall n #'1+ 0))


Line 740: Line 761:


<syntaxhighlight lang="lisp">
<syntaxhighlight lang="lisp">
(defvar zero (lambda (f) (lambda (x) x)))

(defvar one (lambda (f) (lambda (x) (funcall f x))))
(defvar one (lambda (f) (lambda (x) (funcall f x))))


Line 756: Line 779:
(lambda (x) (funcall f (funcall g x))))
(lambda (x) (funcall f (funcall g x))))


(defun church (i)
(defun church (i) ; int -> Church
(if (zerop i) zero (succ (church (1- i)))))
(if (zerop i) zero (succ (church (1- i)))))


(defun unchurch (n)
(defun unchurch (n) ; Church -> int
(funcall (funcall n #'1+) 0))
(funcall (funcall n #'1+) 0))
</syntaxhighlight>
</syntaxhighlight>
Line 778: Line 801:
(defun minus (m n)
(defun minus (m n)
(funcall n #'pred m))
(funcall n #'pred m))
</syntaxhighlight>


<syntaxhighlight lang="lisp">
(defmacro church-if (test then else)
(defmacro church-if (test then else)
`(funcall ,test (lambda () ,then) (lambda () ,else)))
`(funcall ,test (lambda () ,then) (lambda () ,else)))
Line 789: Line 814:
(defun unbool (b)
(defun unbool (b)
(church-if b t nil))
(church-if b t nil))
</syntaxhighlight>


<syntaxhighlight lang="lisp">
(defun is-zero (n)
(defun is-zero (n)
(funcall n (lambda (x) false) true))
(funcall n (lambda (x) false) true))
Line 818: Line 845:


===Further, curried===
===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">
<syntaxhighlight lang="lisp">