Lambda Calculus CONS与Lisp配对实施 [英] Lambda Calculus CONS Pair implementation with Lisp

查看:96
本文介绍了Lambda Calculus CONS与Lisp配对实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现教堂对Lambda Calc. CLisp风格.

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

根据Wikipedia:

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

这2个函数从Int转换为ChurchNumber

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

)

所以,我要做的是:

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

然后:

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

我有:

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

所以,如果我这样做:

(funcall #'FIRST P)

我遇到了这个错误:

*** - 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.

谢谢

推荐答案

  1. 您正在使用#'FIRST.
    这意味着它使用的是 function FIRST,而不是您定义的变量. FIRST(来自标准库)是car的另一个名称,即它返回列表的第一个元素(因此错误提示P不是列表).
    修正:(funcall FIRST P)
    这将导致错误*** - EVAL: undefined function P

  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

这是由您对FIRST的定义引起的:#'(lambda (p) (funcall (p TRUE)))
这意味着什么:返回带有单个参数p的lambda函数.该参数将被忽略.而是调用全局函数p,将变量TRUE的值作为参数传递给(p TRUE).然后将p的结果作为另一个函数调用:(funcall ...).
修复:#'(lambda (p) (funcall p TRUE))
这将导致错误*** - :LAMBDA: variable TRUE has no value

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

那是因为您尚未实际定义TRUEFALSE.
修复:定义TRUEFALSE.

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

这篇关于Lambda Calculus CONS与Lisp配对实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆