Lambda Calculus CONS与Lisp配对实施 [英] Lambda Calculus CONS Pair implementation with 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.
谢谢
推荐答案
-
您正在使用
#'FIRST
.
这意味着它使用的是 function FIRST,而不是您定义的变量.FIRST
(来自标准库)是car
的另一个名称,即它返回列表的第一个元素(因此错误提示P
不是列表).
修正:(funcall FIRST P)
这将导致错误*** - EVAL: undefined function P
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 forcar
, i.e. it returns the first element of a list (hence the error complaining aboutP
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
那是因为您尚未实际定义TRUE
和FALSE
.
修复:定义TRUE
和FALSE
.
That's because you haven't actually defined TRUE
and FALSE
yet.
Fix: Define TRUE
and FALSE
.
这篇关于Lambda Calculus CONS与Lisp配对实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!