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
If you want to define
opandas a function with 2 parameters, like you are trying to, you need to do this:and then:
This is my implementation, based on the original paper http://www.utdallas.edu/~gupta/courses/apl/lambda.pdf, of the
andoperatorλxy.xyFAnd if you do