Question

I'm trying to implement a Church Pair Lambda Calc. style with CLisp.

According with Wikipedia:

pair ≡ λx.λy.λz.z x y

So far, this is my code:

 (defvar PAIR
         #'(lambda(x)
                 #'(lambda(y)
                         #'(lambda(z)
                                 (funcall (funcall z x) y)))) )

These are my FIRST and SECOND Functions:

(defvar FIRST
        #'(lambda(p)
                (funcall(p TRUE)))
)

(defvar SECOND
        #'(lambda(p)
                (funcall(p FALSE)))
)

This 2 functions convert from Int to ChurchNumber

(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))))))

)

So, what I do is:

(setq six (int2church 6))
(setq four (int2church 4))

And then:

(setq P (funcall (funcall PAIR six) four))

And the I've got:

#<FUNCTION :LAMBDA (Y) (FUNCALL (FUNCALL F X) Y)>

So if I do this:

(funcall #'FIRST P)

I've got the this error:

*** - FIRST: #<FUNCTION :LAMBDA (Y) (FUNCALL (FUNCALL F X) Y)> is not a list

I can't see what I am doing wrong. Any help would be appreciated.

Thanks

Was it helpful?

Solution

  1. You're using #'FIRST.
    That means it's using the function FIRST, not the variable you defined. FIRST (from the standard library) is another name for car, i.e. it returns the first element of a list (hence the error complaining about P not being a list).
    Fix: (funcall FIRST P)
    That will cause the error *** - EVAL: undefined function P

  2. This is caused by your definition of FIRST: #'(lambda (p) (funcall (p TRUE)))
    What this means is: Return a lambda function with a single parameter p. That parameter is ignored. Instead, call the global function p passing the value of the variable TRUE as an argument: (p TRUE). Then call the result of p as (another) function: (funcall ...).
    Fix: #'(lambda (p) (funcall p TRUE))
    That will cause the error *** - :LAMBDA: variable TRUE has no value

  3. That's because you haven't actually defined TRUE and FALSE yet.
    Fix: Define TRUE and FALSE.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top