如何应用方案中列表中的匿名函数?
我正在学习Scheme。 下面的代码有什么问题?我想编写一个程序,从列表中获取第一个函数,然后将其应用于数字?
(define num 3)
;;I want to do something like this which returns 3
((λ (x) x)num)
;;but my functions are in a list so this should return3
((first '((λ (x) x) (λ (x) (* x x)))) num)
我收到上述代码的错误:
程序应用:预期程序,给定:(λ (x) x); 参数是:3
当我得到这些类型的输出时,这意味着什么?
当我不应用任何东西时,我会得到很好的输出。
(first '((λ(x) x)(λ(x) (*x x))))
返回 (λ (x) x)
I am learning Scheme. What is wrong with the code below?I want to write a program that takes the first function from the list and then applies that to a number?
(define num 3)
;;I want to do something like this which returns 3
((λ (x) x)num)
;;but my functions are in a list so this should return3
((first '((λ (x) x) (λ (x) (* x x)))) num)
Im getting this error for the above code:
procedure application: expected procedure, given: (λ (x) x); arguments were: 3
What does it mean when I get these kinds of output?
When I dont apply anything, I get a nice output.
(first '((λ(x) x)(λ(x) (*x x))))
returns (λ (x) x)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在用 ' 引用 lambda,因此它不会被评估。
如果您只是在提示符处输入 (λ (x) x),DrScheme 会向您显示
#
,这意味着它实际上已经评估了 lambda,并返回了一个闭包。 通过引用它,您只是为Scheme提供了一个符号列表。如果您想将函数放入列表中,您可以这样做:
引用允许您生成一个列表,是的,但其内容不会被评估。 list 函数在计算所有参数后生成一个列表。
如果您愿意,您还可以准引用该列表:
You're quoting, with ' the lambda, so it isn't being evaluated.
If you just feed in (λ (x) x) at the prompt, DrScheme shows you
#<procedure>
, which means it has actually evaluated the lambda, and given you back a closure. By quoting it, you're giving Scheme just a list of symbols.If you want to put your functions in a list, you can do:
The quote allows you to produce a list, yes, but one whose contents aren't evaluated. The list function produces a list from all of its arguments, after they've been evaluated.
You could also quasiquote the list, if you like:
这些表达方式有什么区别?
杰伊为你回答了这个问题,但我还不能投票给他。
What is the difference between these expressions?
Jay answered it for you but I can't upvote him yet.
(lambda (x) x) 不是一个过程。 它是一种评估过程的形式。 人们对术语的理解有点宽松,经常将 lambda 形式称为过程,作为一种简写。 “塞西不是烟斗。”
(lambda (x) x) isn't a procedure. It's a form that evaluates to a procedure. People are a bit loose with terminology and often call the lambda form a procedure as a kind of shorthand. “Ceci n'est pas une pipe.”