Question

I'm very new with functional programming, lisp and lambda calculus. Im trying to implement the AND operator with Common Lisp Lambda Calc style.

From Wikipedia:

AND := λp.λq.p q p

So far this is my code:

(defvar TRUE #'(lambda(x)#'(lambda(y)x)))
(defvar FALSE #'(lambda(x)#'(lambda(y)y)))

(defun OPAND (p q)
    #'(lambda(f) 
        #'(lambda(p) #'(lambda(q) (funcall p (funcall q(funcall p))))))
)

I found this 2 conversion functions:

(defun church2int(numchurch)
    (funcall (funcall numchurch #'(lambda (x) (+ x 1))) 0)
)

(defun int2church(n)
    (cond
        ((= n 0) #'(lambda(f) #'(lambda(x)x)))
        (t #'(lambda(f) #'(lambda(x) (funcall f
            (funcall(funcall(int2church (- n 1))f)x))))))

)

If I do:

(church2int FALSE)

I've got 0. If I do this:

(church2int TRUE)

I have

#<FUNCTION :LAMBDA (X) (+ X 1)>

Which I think it's ok. But if I do this:

 (church2int (OPAND FALSE FALSE))

I've got:

#<FUNCTION :LAMBDA (Q) (FUNCALL P (FUNCALL Q (FUNCALL P)))>

Where I should have 0. Is there something wrong with my code? Or am I missing something?

Thanks

Was it helpful?

Solution

If you want to define opand as a function with 2 parameters, like you are trying to, you need to do this:

(defun OPAND (p q)
    (funcall (funcall p q) p) )

and then:

(opand false false)
#<FUNCTION :LAMBDA (X) #'(LAMBDA (Y) Y)> ;; which is FALSE

(opand true true)
#<FUNCTION :LAMBDA (X) #'(LAMBDA (Y) X)> ;; which is TRUE

This is my implementation, based on the original paper http://www.utdallas.edu/~gupta/courses/apl/lambda.pdf, of the and operator λxy.xyF

(defvar OPAND 
    #'(lambda(x) 
        #'(lambda(y) 
            (funcall (funcall x y) FALSE) ) ) )

And if you do

(funcall (funcall opand false) false)
#<FUNCTION :LAMBDA (X) #'(LAMBDA (Y) Y)> ;; which is FALSE

(funcall (funcall opand true) true)
#<FUNCTION :LAMBDA (X) #'(LAMBDA (Y) X)> ;; which is TRUE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top