Scheme 使用λ;定义球拍中的功能
以下功能工作:Scheme 使用λ;定义球拍中的功能,scheme,racket,Scheme,Racket,以下功能工作: (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))
(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
λ符号能否用于定义可在多个位置调用的内部(或通用)函数?是的,但您需要像定义任何其他标识符一样定义它
(define (testfn2)
(define contains (λ (sl item) (ormap (λ(x)(equal? item x)) sl)))
(if (contains (list 1 2 3) 2) "yes" "no"))
您的代码创建了一个函数,但(1)它没有绑定到任何东西,(2)它实际上需要三个参数,第一个参数是“包含”。
λ
是lambda
的别名,而不是定义
。“λ符号”是小写希腊字母lambda。