Understand "curry" function

Q&A's, tips, howto's

Understand "curry" function

Postby cameyo » Sun Nov 25, 2018 9:28 am

This is what I understand ...

I can map a function with multiple arguments in this way:
Code: Select all
(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:
Code: Select all
(setq lst '((2 1) (3 4)))

I got an error:
Code: Select all
(map pow lst)
ERR: value expected in function pow : '(2 1)

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

where: 2 = 2^1, 81 = 3^4
Ok, transpose the list of arguments:
Code: Select all
(map (curry apply pow) (transpose lst))
;-> (8 1)

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

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

Code: Select all
(setq lst '((2 1) (3 4)))
(mypow (transpose lst))
;-> (8 1)

Another example:
Code: Select all
(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...
cameyo
 
Posts: 15
Joined: Sun Mar 27, 2011 3:07 pm

Return to newLISP in the real world

Who is online

Users browsing this forum: No registered users and 2 guests

cron