curry in scheme

2019-04-07 18:23发布

I have this curry function:

(define curry
(lambda (f) (lambda (a) (lambda (b) (f a b)))))

I think it's like (define curry (f a b)).

my assignment is to write a function consElem2All using curry,which should work like

(((consElem2All cons) 'b) '((1) (2 3) (4)))
>((b 1) (b 2 3) (b 4))

I have wrote this function in a regular way:

(define (consElem2All0 x lst) 
  (map (lambda (elem) (cons x elem)) lst))

but still don't know how to transform it with curry. Can anyone help me?

thanks in advance

bearzk

3条回答
干净又极端
2楼-- · 2019-04-07 18:51
(define (consElem2All0 x lst) 
  (map ((curry cons) x) lst))
查看更多
虎瘦雄心在
3楼-- · 2019-04-07 18:57

You should begin by reading about currying. If you don't understand what curry is about, it may be really hard to use it... In your case, http://www.engr.uconn.edu/~jeffm/Papers/curry.html may be a good start.

One very common and interesting use of currying is with functions like reduce or map (for themselves or their arguments).

Let's define two currying operators!

(define curry2 (lambda (f) (lambda (arg1) (lambda (arg2) (f arg1 arg2)))))
(define curry3 (lambda (f) (lambda (arg1) (lambda (arg2) (lambda (arg3) (f arg1 arg2 arg3))))))

Then a few curried mathematical functions:

(define mult (curry2 *))
(define double (mult 2))

(define add (curry2 +))
(define increment (add 1))
(define decrement (add -1))

And then come the curried reduce/map:

(define creduce (curry3 reduce))
(define cmap (curry2 map))

Using them

First reduce use cases:

(define sum ((creduce +) 0))
(sum '(1 2 3 4)) => 10

(define product (creduce * 1))
(product '(1 2 3 4)) => 24

And then map use cases:

(define doubles (cmap double))
(doubles '(1 2 3 4)) => (2 4 6 8)

(define bump (cmap increment))
(bump '(1 2 3 4)) => (2 3 4 5)

I hope that helps you grasp the usefulness of currying...

查看更多
可以哭但决不认输i
4楼-- · 2019-04-07 19:04

So your version of curry takes a function with two args, let's say:

(define (cons a b) ...)

and turns that into something you can call like this:

(define my-cons (curry cons))
((my-cons 'a) '(b c)) ; => (cons 'a '(b c)) => '(a b c)

You actually have a function that takes three args. If you had a curry3 that managed 3-ary functions, you could do something like:

(define (consElem2All0 the-conser x lst) ...)

(like you did, but allowing cons-like functions other than cons to be used!)

and then do this:

(define consElem2All (curry3 consElem2All0))

You don't have such a curry3 at hand. So you can either build one, or work around it by "manually" currying the extra variable yourself. Working around it looks something like:

(define (consElem2All0 the-conser)
  (lambda (x lst) ...something using the-conser...))
(define (consElem2All the-conser)
  (curry (consElem2All0 the-conser)))

Note that there's one other possible use of curry in the map expression itself, implied by you wrapping a lambda around cons to take the element to pass to cons. How could you curry x into cons so that you get a one-argument function that can be used directly to map?...

查看更多
登录 后发表回答