[racket] Simplified scut-like macro

From: Paul Pereira (pjspereira at gmail.com)
Date: Thu Feb 23 15:44:16 EST 2012

Thanks Jay and Gary,

I remember seeing something like this in an APL variant and it was one
of the few features I didn't know how to  translate into scheme.

Cheers,
Paul Pereira

On 23 February 2012 14:36, Jay McCarthy <jay.mccarthy at gmail.com> wrote:
> I think you should look at the Macro Stepper output for your failing
> example, because you'll see that the code that references y appears in
> the branch that doesn't have access to a y. You'd want to do something
> in the macro to determine which function to compile to.
>
> Jay
>
> p.s. I'm glad you like super cut. =)
>
> On 2/22/12, Paul Pereira <pjspereira at gmail.com> wrote:
>> I am new to macro writing and am trying to do the following:
>>
>> (fun body ...)
>>  =>  (lambda (x) body ...)
>> or (lambda (x y) body ...)
>> or (lambda (x y z) body ...)
>>
>> where x, y, z are taken from the scope of body.
>> Right now, I am just trying to use case-lambda to switch between
>> these. I guess I am trying for a much simpler version of scut (
>> https://github.com/jeapostrophe/exp/blob/master/scut.ss ), but I don't
>> understand that macro yet.
>>
>> What I have so far does not work:
>>
>> (define-syntax (fun stx)
>>   (syntax-case stx ()
>>     ((fun body rest ...)
>>      (with-syntax ((x (datum->syntax #'body 'x))
>>                    (y (datum->syntax #'body 'y))
>>                    (z (datum->syntax #'body 'z)))
>>         #'(case-lambda ((x) body rest ...)
>>                        ((x y) body rest ...)
>>                        ((x y z) body rest ...))))))
>>
>> (map (fun x) '(1 2 3))  => '(1 2 3)    ... ok
>> (map (fun (+ x x)) '(1 2 3))  => '(2 4 6) ... ok
>> (map (fun (+ x y)) '(1 2 3) '(1 2 3))  ... y is an unbound identifier
>>
>> Do you have any suggestions? Thanks!
>>
>> Paul
>> ____________________
>>   Racket Users list:
>>   http://lists.racket-lang.org/users
>>
>
>
> --
> Jay McCarthy <jay at cs.byu.edu>
> Assistant Professor / Brigham Young University
> http://faculty.cs.byu.edu/~jay
>
> "The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.