...
Currying is technically the process in which a function, which takes n input parameters, creates another function, which only takes n- input parameters. Mathamatically, this is a type of process is called a "transformation". We can see the process in the "make_addX" example above, where the make-addX function is a function of 2 input parameters when we consider that in order to get a final result that is a number, we must supply 2 input parameters as such: ((make-addX num1) num2). See the "check-expect" tests for examples of this. However, the return value of the make-addX function is a function of only one input parameter. To get a numerical result then, we only need to supply 1 input parameter:
Code Block |
---|
;; two input parameters needed to make numerical result:
((make-addX 5) 3) ;; --> 8
;; but looking just at the return value of make-addX
(define add5 (make-addX 5))
;; only one input parameter is needed:
(add5 3) ;; --> 8 |
What does this mean? The effect is that the n'th input parameter in the original function, which by its very nature of being an input parameter, is a variant entity with regards to the usage of that function, "make-addX" above, but has been tranformed into an invariant entity with respect to the usage of the returned function, "add5" above.
Code Block |
---|
(define add7 (make-addX 7)) ;; 7 is the value of the variant parameter "x" for make-addX
(define add42 (make-addX 42)) ;; 42 is the value of the variant parameter "x" for make-addX
(add7 10) ;; --> 17 because 7 is invariant now for add7
(add7 -15) ;; --> -8 because 7 is invariant now for add7
(add42 10) ;; --> 52 because 42 is invariant now for add42
(add42 -15) ;; --> 27 because 42 is invariant now for add42 |
One way to look at the currying process is to say that the returned lambda of make-addX "captures" the value of "x" at the moment that it is created.
All of this is part of the notion of a function's "closure", a topic we will discuss more formally very soon,