简体   繁体   English

如何将列表中的每个元素应用于方案中的函数?

[英]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))))

OR just use map :或者只使用map

(map proc lst ...+)

gl高尔

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

相关问题 如何将布尔函数应用于列表中的每个元素? - How can I apply a boolean function to each element in a list? 如何在列表的每个元素之间应用函数? - How to apply a function between each element of a list? 如何将 function 应用于字符列表的每个元素 - How to apply a function to each element of a list of chars 将 function 应用于列表的每个元素 - Apply function to each element of a list 如何将 function 应用于列表中的每个元素,然后制作输出列表? - How to apply a function to each element in a list, then make a list of the outputs? 当有两个输入时,如何将函数应用于列表的每个元素? 哈斯克尔 - How do I apply a function to each element of list, when it has two inputs? Haskell 如何在没有 for 循环或 lambda 函数的情况下将 function 应用于列表的每个元素? - How to apply a function to each element of a list without for loops or lambda functions? 将函数应用于矩阵列表的每个元素 - Apply function on each element of a list of matrices 如何将Scheme中的function应用于另一个function返回的arguments列表? - How to apply a function in Scheme to a list of arguments returned by another function? Python:对于每个列表元素,在列表中应用一个函数 - Python: For each list element apply a function across the list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM