Filtering through a list of numbers

余生长醉 提交于 2019-12-20 03:11:35

问题


This is my function for curry:

(define (curry g)
  (lambda(x)
    (lambda(y)
      (g x y))))

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)))

But it only produces the list of numbers equal to 1.

ex. (filter-numbers (list 1 2 3)) -> (list 1)

I want to get (list 2 3) but have no idea how. Can anyone help?


回答1:


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)))

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)



回答2:


The filter function retains elements which satisfy the test. You have to negate your predicate.




回答3:


You could create a function my-compose as follows:

(define (my-compose f g)
  (lambda (x) (f (g x))))

and with your curry function:

(define (curry g)
  (lambda (x) (lambda (y) (g x y))))

You can create a filter function (without using filter-not)

(define (filter-numbers lst) 
  (filter (my-compose not ( (curry  =) 1)) lst))


(filter-numbers (list 1 2 1 1 3 4 2 ))  => (list 2 3 4 2)


来源:https://stackoverflow.com/questions/33869174/filtering-through-a-list-of-numbers

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!