[plt-scheme] macro expension order

From: Robby Findler (robby at cs.uchicago.edu)
Date: Mon Jul 9 20:51:19 EDT 2007

Chongkai: Jens Axel is making the reasonable (indeed wise!) suggestion
that you represent ML values with structs, not lists. This will be
more efficient and will make it easier to avoid bugs. All in all, a
good idea.

Robby

On 7/9/07, Chongkai Zhu <czhu at cs.utah.edu> wrote:
> The problem is: to _compile_ SML code into PLT Scheme, not to manually
> translate ML.
>
> According to SML syntax, in (list NONE n), there is no obvious
> difference bewetween NONE and n: they are both normal symbol.
>
> Chongkai
>
> Jens Axel Søgaard wrote:
> > Chongkai Zhu skrev:
> >> Hi all,
> >>
> >> I have the following program;
> >>
> >>  (define-syntax NONE (syntax-id-rules () (NONE 'NONE)))
> >>  (define-syntax SOME (syntax-rules () ((SOME g57951) (list 'SOME
> >> g57951))))
> >>  (define expt
> >>    (match-lambda*
> >>      ((list NONE n) (expt (SOME 2) n))
> >>      ((list (list 'SOME b) 0) 1)
> >>      ((list (list 'SOME b) n) (if (= (mod n 2) 0) (expt (SOME (* b
> >> b)) (/ n 2)) (* b (expt (SOME b) (- n 1)))))))
> >>
> >> It is automatically translated from the following SML program:
> >>
> >> datatype 'a option = NONE | SOME of 'a
> >>
> >> fun expt (NONE, n) = expt (SOME 2, n)
> >>   | expt (SOME b, 0) = 1
> >>   | expt (SOME b, n) =
> >>     if n mod 2 = 0 then
> >>        expt (SOME (b*b), n div 2)
> >>     else
> >>        b * expt (SOME b, n-1)
> >
> >
> > Why not represent NONE and SOME as structures?
> >
> > (define-struct NONE ())
> > (define-struct SOME (b))
> >
> > (require (lib "plt-match.ss"))
> >
> > (define expt
> >   (match-lambda*
> >     [(list (struct NONE ()))
> >      (expt (make-SOME 2) n)]
> >     [(list (struct SOME (b)) 0)
> >      1]
> >     [(list (struct SOME (b)) n)
> >      (if (= (modulo n 2) 0)
> >          (expt (make-SOME (* b b)) (quotient n 2))
> >          (* b (expt (make-SOME b) (- n 1))))]))
> >
> > (expt (make-SOME 2) 0) ; => 1
> > (expt (make-SOME 2) 1) ; => 2
> > (expt (make-SOME 2) 2) ; => 4
> > (expt (make-SOME 2) 3) ; => 8
> >
> >
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>

Posted on the users mailing list.