方案/球拍过滤器/映射多个参数 [英] Scheme/Racket filter/map multiple arguments
问题描述
(define(foo lst x)
(filter function lst)
但是函数
需要2个参数(和函数
给了我),其中一个是它将使用的 lst
列表,另一个是 x
。语法上,我将如何更改该行以在第二个参数中传递?对不起,我是Scheme / DrRacket的新手。
试试这个,使用 咖喱
:
(define(foo lst x)
(filter(curry function x)lst))
也就是说,假定函数
作为第一个参数 x
和作为 lst
中每个元素的第二个参数。换句话说,上面的内容等价于:
(define(foo lst x)
(filter(lambda(e)(function xe))
lst))
无论哪种方式:诀窍(称为 currying )是创建一个接收单个参数的新函数,并将其传递给原函数,该函数使用给定的 x
value。
在你的问题中,我们不应该以什么顺序通过参数,但是一旦你理解了基本原理在这里工作,你就能弄明白。
Lets say I want to do the following:
(define (foo lst x)
(filter function lst)
but function
takes in 2 arguments (and function
was given to me), one being the list lst
it will use, and the other being x
. Syntactically, how would I change that line to pass in the second argument? Sorry I am new to Scheme/DrRacket.
Try this, using curry
:
(define (foo lst x)
(filter (curry function x) lst))
That is, assuming that function
takes as first parameter x
and as second parameter each one of the elements in lst
. In other words, the above is equivalent to this:
(define (foo lst x)
(filter (lambda (e) (function x e))
lst))
Either way: the trick (called currying) is to create a new function that receives a single argument, and passes it to the original function, which has the other argument fixed with the given x
value.
In your question, it's not clear in which order we should pass the arguments, but once you understand the basic principle at work here, you'll be able to figure it out.
这篇关于方案/球拍过滤器/映射多个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!