SRFI like cut (SRFI-26) but with ordered arguments
Jakub T. Jankiewicz 14 Aug 2021 07:30 UTC
Can you use [SRFI-26](https://srfi.schemers.org/srfi-26/srfi-26.html) or
other SRFI to quickly create a function with changed order of arguments?
There should be syntax like:
(define take (cut take <2> <1>))
will swap the arguments in take. With this you can make complex change of the
original function that need different order of arguments. For example to be
able to curry that function.
The take function from SRFI-1 is specified as (take <list> <n>) so you can
use:
(define take2 (cut take <> 2))
But it would be much better to have function that you can call curry on:
(define take2 (curry take 2))
curry can be a function and could be used in places where macro can't.
PS: is there SRFI that define curry for Scheme? Or something that work
similarly?
In my implementation I'm using feature of JavaScript functions that has
length property that return number of parameters the function accepts.
So the function can keep returning lambda expression until all parameters are
passed.
(define (add a b c d)
(+ a b c d))
(define sum (curry add))
((sum 1 2) 4 5)
;; ==> 12
NOTE that the original curry definition returns single argument functions,
but you still need to get length of the arguments the function accepts.
--
Jakub T. Jankiewicz, Web Developer
https://jcubic.pl/me