[英]How can I apply each element in a list to a function in scheme?
The function applyToAll is suppose to take in a function and a List, then take the car of the list and apply each element to the fuction.函数 applyToAll 假设接收一个函数和一个 List,然后获取列表的 car 并将每个元素应用到函数中。
This is what I have worked out so far:这是我迄今为止的工作:
(define applyToAll(lambda (f L)
(cond
((null? L) '())
(#t (cons (L) (applyToAll f(car L))))
)))
I'm not sure what I am doing wrong.我不确定我做错了什么。 A fuction call would look like
函数调用看起来像
(applyToAll (lambda (n) (* n n)) '(1 2 3) )
and it would return它会回来
(1 4 9)
Instead it returns: function call: expected a function after the open parenthesis, but received (list 1 2 3)相反,它返回:函数调用:期望在左括号后有一个函数,但已收到(列表 1 2 3)
Any help as to why my code is not working?关于为什么我的代码不起作用的任何帮助?
Thanks谢谢
It sounds like you are trying to implement map
.听起来您正在尝试实施
map
。
The error you are getting is because you are calling a list as though it's a function (lst1)
您收到的错误是因为您正在调用一个列表,就好像它是一个函数一样
(lst1)
( )
this means function call in scheme ( )
这意味着方案中的函数调用
You are making the same mistake here:你在这里犯了同样的错误:
(#t (cons (L) (applyToAll f(car L))))
the right way to apply is:正确的申请方式是:
(function arg0 arg1 ... argn)
You need to apply f to each element in the list like so:您需要将 f 应用于列表中的每个元素,如下所示:
(cons (f (car L)) (applyToAll f (cdr L))))
(map proc lst ...+)
gl高尔
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.