[racket] a little macro exercise

From: Jay McCarthy (jay.mccarthy at gmail.com)
Date: Sat Oct 9 09:13:55 EDT 2010

I don't really like have the call-with-values and apply there, so
here's another version. It makes the macro a bit longer with the
additional case and has the pattern duplicated once, but it seems
worth it:

(define-syntax cas-cad-e
  (syntax-rules ()
    [(_ e) (begin e (void))]
    [(_ e [(n ...) code ...] ... [(n_l ...) code_l ...])
     (let/ec esc
       (syntax-parameterize
        ([break (make-rename-transformer #'esc)])
        (let* ([tmp e]
               [earlier? #f]
               [earlier?
                (if (or earlier? (equal? tmp n) ...)
                    (begin code ... #t)
                    earlier?)]
               ...)
          (when (or earlier? (equal? tmp n_l) ...)
            code_l ...))))]))

Regarding Shriram's bug. The only thing that occurs to me is that
you'd want eqv? and a ' on the ns, to be more like case.

Jay

On Fri, Oct 8, 2010 at 10:39 PM, Jay McCarthy <jay.mccarthy at gmail.com> wrote:
> You got me
>
> Sent from my iPhone
>
> On Oct 8, 2010, at 10:33 PM, Eli Barzilay <eli at barzilay.org> wrote:
>
>> 8 minutes ago, Jay McCarthy wrote:
>>> Alright, here's the version with no mutation:
>>
>> (cas-cad-e 1 [(1) (values 1 2 3)])
>>
>> In other words:
>>
>> (define-syntax-rule (cas-cad-e e [(n ...) code ...] ...)
>>  (let/ec esc
>>    (syntax-parameterize ([break (make-rename-transformer #'esc)])
>>      (let*-values ([(tmp) e]
>>                    [(earlier? ret) (values #f (void))]
>>                    [(earlier? ret)
>>                     (if (or earlier? (equal? tmp n) ...)
>>                       (values #t (call-with-values (lambda () code ...) list))
>>                       (values earlier? ret))]
>>                    ...)
>>        (apply values ret)))))
>>
>> --
>>          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
>>                    http://barzilay.org/                   Maze is Life!
>



-- 
Jay McCarthy <jay at cs.byu.edu>
Assistant Professor / Brigham Young University
http://teammccarthy.org/jay

"The glory of God is Intelligence" - D&C 93


Posted on the users mailing list.