Functional programming 球拍:将字符串替换应用于列表
给我错误信息Functional programming 球拍:将字符串替换应用于列表,functional-programming,scheme,racket,Functional Programming,Scheme,Racket,给我错误信息 (apply string-replace ";" "" '("a" "b;" "c")) 据我所知,问题是列表被cons绑定到字符串替换函数的参数,而我想将该函数应用到列表的每个元素 将字符串替换应用于列表的正确方法是什么 string-replace: arity mismatch; the expected number of arguments does not matc
(apply string-replace ";" "" '("a" "b;" "c"))
据我所知,问题是列表被cons
绑定到字符串替换
函数的参数,而我想将该函数应用到列表的每个元素
将字符串替换应用于列表的正确方法是什么
string-replace: arity mismatch;
the expected number of arguments does not match the given number
expected: 3 plus an optional argument with keyword #:all?
given: 5
arguments...:
可以工作,但返回“b;bb;
,而我希望“b”
要将函数应用于列表的所有元素,请使用映射
():
产生
(map (lambda (str) (string-replace str ";" ""))
'("a" "b;" "c"))
要将函数应用于列表的所有元素,请使用map
():
产生
(map (lambda (str) (string-replace str ";" ""))
'("a" "b;" "c"))
非常感谢,这很有效!为了理解答案:为什么像(apply+”(1 2 3))
这样的东西不需要映射?因为您正在应用一个函数(++
),该函数可以接收参数列表(这样您就可以编写(+1 2 3)
)。对于字符串替换
,函数只需要一个字符串参数。因此,如果要将其应用于参数列表,则必须“重复”应用程序“谢谢你的解释。我没有真正理解这个基本概念。更详细地解释它。事实上,如果您查看答案中的示例,您可以看到应用于列表的函数不是string replace
,而是(lambda(str)(string replace str“;”)
。非常感谢,这很有效!为了理解答案:为什么像(apply+”(1 2 3))
这样的东西不需要map
?因为您正在应用一个可以接收参数列表的函数(++
)(这样您就可以编写(+1 2 3)
)。在stringreplace
的情况下,函数只需要一个字符串参数。因此,如果要将其应用于参数列表,则必须为每个参数“重复”应用程序。感谢您的解释。我没有真正理解这个基本概念。事实上,如果您查看答案中的示例,您可以看到应用于列表的函数不是string replace
,而是(lambda(str)(string replace str“;”)
)。
'("a" "b" "c")