As I learn scheme and racket I find myself repeating this pattern again and again. Where I have a recursive function where some of the parameters to the function change but some of the parameters do not. I build an outer function that takes all the parameters and within that define an inner function that takes only the changing parameters and recur on that.
As a concrete example heres a case based somewhat on a function exercise in “The Little Schemer”
;inserts an item to the right of an element in a list
(define (insert-to-right new old lat)
(define (insert-to-right lat)
(cond
[(null? lat) lat]
[(eq? old (car lat) ) (cons old (cons new (cdr lat)))]
[else (cons (car lat) (insert-to-right (cdr lat)))]))
(insert-to-right lat))
Is it possible to build a macro define* and an operator (for example a vertical bar) such that I would type:
(define* (insert-to-right new old | lat)
(cond
[(null? lat) lat]
[(eq? old (car lat) ) (cons old (cons new (cdr lat)))]
[else (cons (car lat) (insert-to-right (cdr lat)))]))
and this would then expand into the first form with all the parameters being passed to the outer function but only the parameters after the vertical bar being passed to the inner loop.
After playing around I’ve built a macro that does what I want.
In the define* statement it doesn’t use a separator between the inner and outer parameters (as I originally tried to do) but puts the inner and outer parameters in the define* statement into separate lists which I think is more idiomatic Scheme/Racket.