简体   繁体   English

在 Racket 中使用 λ 定义函数

[英]Using λ to define function in Racket

Following function works:以下功能有效:

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

(testfn)

Output:输出:

"yes"

But following, which uses λ symbol, does not:但是使用 λ 符号的以下不会:

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

Error is:错误是:

contains: unbound identifier in module in: contains

Can λ symbol be used to define inner (or general) functions which may be called at multiple places?可以使用 λ 符号来定义可以在多个地方调用的内部(或通用)函数吗?

Yes, but you need to define it like you would with any other identifier.是的,但您需要像使用任何其他标识符一样定义它。

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

Your code creates a function but (1) it is not bound to anything and (2) it actually takes three arguments, the first of which is "contains".您的代码创建了一个函数,但(1)它没有绑定到任何东西,(2)它实际上需要三个参数,第一个是“包含”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM