|
|
|
|
|
by shawn_w
86 days ago
|
|
A bunch of Scheme implementations define little-known syntax for partial application[0] that lets you put limits on how many arguments have to be provided at each application step. Using the article's add example: (define (((add x) y) z) (+ x y z))
(define add1 (add 1))
(define add3 (add1 2))
(add3 3) ; => 6
it gets tedious with lots of single-argument cases like the above, but in cases where you know you're going be calling a function a lot with, say, the first three arguments always the same and the fourth varying, it can be cleaner than a function of three arguments that returns an anonymous lambda of one argument. (define ((foo a b c) d)
(do-stuff))
(for-each (foo 1 2 3) '(x y z))
vs (define (foo a b c)
(lambda (d) (do-stuff)))
(for-each (foo 1 2 3) '(x y z))
There's also a commonly supported placeholder syntax[1]: (define inc (cut + 1 <>))
(inc 2) ; => 3
(define (foo a b c d) (do-stuff))
(for-each (cut foo 1 2 3 <>) '(x y z))
And assorted ways to define or adapt functions to make fully curried ones when desired. I like the "make it easy to do something complicated or esoteric when needed, but don't make it the default to avoid confusion" approach.[0]: https://srfi.schemers.org/srfi-219/srfi-219.html [1]: https://srfi.schemers.org/srfi-26/srfi-26.html |
|