I can define church numerals fairly easy using scheme:
> (define f (lambda (x) x))
> (f f) ;0
#<procedure:f>
> (f (f f)) ;1
#<procedure:f>
However, this doesn’t make it very easy to recognize that (f f) is 0 and (f (f f)) is 1. Is there a way that I can make these numerals more readable? What would be ideal is this:
> (f f)
0
> (f (f f))
1
The example is in scheme, but I’ll take an answer in any lisp.
First let’s define real church numerals which have the desirable property that
0 != 1:So
zerois the church representation of 0,(succ zero)of 1,(succ (succ zero))of 2 and so on.Now since those are just functions, there is no way to tell the repl to display them as numbers, but you can define a function cn-to-int which converts church-numerals to ints which can then be displayed normally: