I'm stepping through a procedure that was provided in another answer (https://stackoverflow.com/a/68013528/651174), and I'm struggling trying to finish the substitutions in the procedure. Here is where I'm at now:
; main function
(define (curry num func)
(cond ((= num 1) func)
(else (lambda (x) (curry (- num 1)
(lambda args (apply func (cons x args))))))))
And here is the call I'm doing:
(define (add-3 x y z) (+ x y z))
(add-3 100 200 300)
; 600
((((curry 3 add-3) 100) 200) 300)
; 600
Here is my attempt at substituting through the code to trace how the function works:
; Sub 1/3 (curry 3 add-3)
(lambda (x) (curry (- 3 1)
(lambda args (apply add-3 (cons x args)))))
; verify it works
((((lambda (x) (curry (- 3 1)
(lambda args (apply add-3 (cons x args))))) 100) 200) 300)
; 600 -- OK
; Sub 2/3 (curry 2 <func>)
; <func> = (lambda args (apply add-3 (cons x args)))
(lambda (x)
(lambda (x) (curry (- 2 1)
(lambda args (apply (lambda args (apply add-3 (cons x args))) (cons x args))))))
; verify it works
((((lambda (x)
(lambda (x) (curry (- 2 1)
(lambda args (apply (lambda args (apply add-3 (cons x args))) (cons x args)))))) 100) 200) 300)
; 700 -- Wrong output
I am guessing the 700
value has to do with me having two lambda (x)
's and not properly enclosing them or something. What would be the proper way to do the above substitution?