Understand "curry" function

Started by cameyo, November 25, 2018, 01:28:10 AM

Previous topic - Next topic

cameyo

This is what I understand ...



I can map a function with multiple arguments in this way:
(map pow '(2 1) '(3 4))
;-> (8 1)

where: 8 = 2^3, 1 = 1^4

But, if the list of arguments are within a list:
(setq lst '((2 1) (3 4)))
I got an error:
(map pow lst)
ERR: value expected in function pow : '(2 1)

I use "curry" to solve this problem:
(map (curry apply pow) lst)
;-> (2 81)

where: 2 = 2^1, 81 = 3^4

Ok, transpose the list of arguments:
(map (curry apply pow) (transpose lst))
;-> (8 1)

This is equivalent to:
(map (lambda(x) (apply pow x)) (transpose lst))
;-> (8 1)

We can define a user function too:
(define (mypow lst)
  (if (null? lst) '()
      (cons (pow (nth '(0 0) lst) (nth '(0 1) lst)) (mypow (rest lst)))
  )
)

(setq lst '((2 1) (3 4)))
(mypow (transpose lst))
;-> (8 1)

Another example:
(map max '(3 5) '(2 7))
;-> (3 7)
(map (curry apply max) '((3 5) (2 7)))
;-> (5 7)
(map (curry apply max) (transpose '((3 5) (2 7))))
;-> (3 7)


Did I get it right?

cameyo

p.s. sorry for poor english...