2016-09-15 91 views
-2

定义功能以下功能的工作原理:使用λ球拍

(define (testfn) 
    (define (contains sl item) (ormap (λ(x)(equal? item x)) sl)) 
    (if (contains (list 1 2 3) 2) "yes" "no")) 

(testfn) 

输出:

"yes" 

但以下,使用λ符号,并不:

(define (testfn2) 
    (λ (contains sl item) (ormap (λ(x)(equal? item x)) sl)) 
    (if (contains (list 1 2 3) 2) "yes" "no")) 

错误是:

contains: unbound identifier in module in: contains 

λ符号可以用来定义可能在多个地方调用的内部(或一般)函数吗?

+3

'λ'是'lambda'的别名,而不是'define'。 –

+0

“λ符号”是小写的希腊字符* lambda *。 – molbdnilo

回答

0

是的,但您需要像使用任何其他标识符一样来定义它。

(define (testfn2) 
    (define contains (λ (sl item) (ormap (λ(x)(equal? item x)) sl))) 
    (if (contains (list 1 2 3) 2) "yes" "no")) 

你的代码创建了一个功能,但(1)它没有被绑定到任何东西,(2),它实际上有三个参数,其中第一个就是“载”。