How can I make Church figures more understandable for a person in lisp?

I can quite easily figure out the church numbers using the diagram:

> (define f (lambda (x) x))
> (f f) ;0
#<procedure:f>
> (f (f f)) ;1
#<procedure:f>

      

However, that doesn't make it easy to figure out what (f f)

is 0 and (f (ff)) is 1. Is there a way I can make these numbers more readable? What would be ideal:

> (f f)
0
> (f (f f))
1

      

An example is given in the diagram, but I will answer in any lisp.

+2


a source to share


1 answer


First, we define the real church figures that have the desired property that 0 != 1

:

(define zero (lambda (f x) x))
(define (succ cn) (lambda (f x) (f (cn f x))))

      

So zero

is the church representation of 0, (succ zero)

1, (succ (succ zero))

of 2, etc.



Now, since these functions are just functions, there is no way to tell the replica to display them as numbers, but you can define a cn-to-int function that converts the number of characters in cells to int, which can then display normally: / p>

> (define (cn-to-int cn) (cn (lambda (x) (+ x 1)) 0))
> (cn-to-int zero)
0
> (cn-to-int (succ zero))
1
> (cn-to-int (succ (succ zero)))
2

      

+9


a source







All Articles