[racket] handling curried functions in a 'define' macro

From: Asumu Takikawa (asumu at ccs.neu.edu)
Date: Sat Feb 15 20:42:06 EST 2014

On 2014-02-15 17:05:34 -0800, Matthew Butterick wrote:
> But how do I handle curried function definitions? Since they can be
> nested to arbitrary depth, I'm puzzled. Is there a way to match them
> directly in syntax-case? Or do I need a preliminary step of converting
> them into the basic define form, like so:

This depends on what you want to do. If you're familiar with
`syntax-parse`, you can take a look at some code I wrote for part of
racket/match that matches function headers:

  https://github.com/plt/racket/blob/master/racket/collects/racket/match/define-forms.rkt#L17

Further down, the `define/match` macro uses these helpers to get the
list of formals to bind using `match`.

If you are more familiar with `syntax-case` instead, you can write a
helper function that traverses the header and collects the argument in
some form that makes sense for your use-case.

Finally, there's also the `normalize-definition` function which can be
handy. For example, if you want to create a new `define` form that uses
a different kind of lambda you can write a macro like this:

  (define-syntax (my-define stx)
    (syntax-case stx ()
      [(_ . rst)
       (define-values (id rhs)
         (normalize-definition stx #'my-lambda))
       #'(define #,id #,rhs)]))

which works like `define` except expands into uses of `my-lambda`.

(the docs should provide an example of this too)

Cheers,
Asumu

Posted on the users mailing list.