I've enjoyed the discussion. Curry is a common idiom in the javascript
world (my current day job) so it was very interesting to explore it here.
Being relatively new to picolisp I have a lot to learn about built-in
functionality so exploring these ideas, with feedback from Alex and others
more familiar, is very productive!
Below is YAC (Yet another curry)...
A simplication, that would allow more natural writing, would be to use @
instead
of the current Args list of provided params to curri2. I am still getting
the hang of that...
/Lindsay
I apologise if it is a lot of code to post here, but this was also great
opportunity to apply a bit of what I have been learning from the "Lisp 1.5
Programmer's Manual"
# note: pairlis2, sublis are functions are adapted from the manual to
implement 'curri2'
# pairlis2: gives a list of pairs of corresponding elements
# of the lists x and y, and appends this to the list a.
# (pairlis '(A B C) '(1 2) () ) -> ((A . 1) (B . 2))
(de pairlis2 (X Y A)
(cond
((null X) A)
((null Y) A)
(T
(cons
(cons (car X) (car Y))
(pairlis2 (cdr X) (cdr Y) A) ) ) ) )
# 'A is an assoc list ((u . v)..).
# sublis: treats the u's as variables
# when they occur in Y, and substitutes the
# corresponding v's from the pair list
(de sublis (A Y)
(let
(sub2
'((A Z)
(cond
((null A) Z)
((= (caar A) Z) (cdar A))
(T (sub2 (cdr A) Z)) ) ) )
(cond
((atom Y) (sub2 A Y))
(T
(cons
(sublis A (car Y))
(sublis A (cdr Y)) ) ) ) ) )
# And now we can make curry too!
(de curri2 (Fun Args)
(let
(Par (pairlis2 (car Fun) Args)
dropP
'((X Y)
(cond
((null Y) X)
(T (dropP (cdr X) (cdr Y))) ) ) )
(list
(dropP (car Fun) Args)
(car (sublis Par (cdr Fun))) ) ) )
-> curri2
: (curri2 '((X Y) (+ X Y)))
-> ((X Y) (+ X Y))
: ('((X Y) (+ X Y)) 1 2)
-> 3
: (curri2 '((X Y) (+ X Y)) (1))
-> ((Y) (+ 1 Y))
: ('((Y) (+ 1 Y)) 2)
-> 3
: (curri2 '((X Y) (+ X Y)) (1 2))
-> (NIL (+ 1 2))
: ('(NIL (+ 1 2)))
-> 3