通过数字列表过滤 [英] Filtering through a list of numbers
问题描述
这是我的咖喱函数:
(define (curry g)
(lambda(x)
(lambda(y)
(g x y))))
我正在尝试使用 curry 函数生成一个不等于 1 的数字列表.
I'm trying to produce a list of numbers not equal to 1 using the curry function.
到目前为止我所拥有的是:
What I have so far is:
(define filter-numbers ((curry filter)
((curry equal?) 1)))
但它只生成等于 1 的数字列表.
But it only produces the list of numbers equal to 1.
例如.(filter-numbers (list 1 2 3)) -> (list 1)
ex. (filter-numbers (list 1 2 3)) -> (list 1)
我想获得(列表 2 3)但不知道如何获得.有人可以帮忙吗?
I want to get (list 2 3) but have no idea how. Can anyone help?
推荐答案
试试这个 - 这是 Racket 中的正确方法,使用内置的 curry
和 href="http://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Flist..rkt%29._filter-not%29%29" rel="nofollow">filter-not
程序:
Try this - is the right approach in Racket, using the built-in curry
and filter-not
procedures:
(define filter-numbers
(curry filter-not (curry equal? 1)))
或者,使用您的 curry
实现:
Alternatively, using your implementation of curry
:
(define filter-numbers
((curry filter-not)
((curry equal?) 1)))
无论哪种方式,它都按预期工作:
Either way, it works as expected:
(filter-numbers '(1 2 3 4 5 1 7 1))
=> '(2 3 4 5 7)
这篇关于通过数字列表过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!