Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

[Scheme]Letrec Macro [fap][fap][fap]

Name: Anonymous 2009-12-23 22:50

From http://community.schemewiki.org/?scheme-faq-macros

I'm surprised that I have never came across this before today.
I Came!!
There is a bug in the R5RS sample implementation of letrec: It expands into an error if there are internal definitions in the body of the letrec. Petrofsky offers the following fixed and more concise definition exploiting internal defines:

 
 (define-syntax letrec
   (syntax-rules ()
     ((_ ((var init) ...) . body)
      (let ()
        (define var init)
        ...
        (let () . body)))))

         

Note that the use of define in the above does not result in a circular macro definition. R5RS states that internal defines are equivalent to some suitable letrec expression, but it is not actually possible to define define as an R5RS macro. Hence define must be regarded as primitive rather than derived syntax. Still, there are some Scheme implementations which only provide a primitive top-leveldefine and implement internal defines in terms of letrecs as part of a low-level macro expansion process that gets applied to all expressions. In such implementations the following, more complex, define-free implementation of letrec can be used:

 
 (define-syntax letrec
   (syntax-rules ()
     ((_ ((var init) ...) . body)
      (let ((var 'undefined) ...)
        (let ((var (let ((temp init)) (lambda () (set! var temp))))
              ...
              (bod (lambda () . body)))
          (var) ... (bod))))))

Name: Anonymous 2009-12-23 22:55

You should probably watch this talk too (assuming you have 40 mins) http://video.google.com/videoplay?docid=-6899972066795135270&hl=en#

Name: Anonymous 2009-12-23 23:14

I am trying to learn Common Lisp right now.  I am quite terrible at it.

Name: Anonymous 2009-12-24 0:13

>>3
What book are you following? I'd suggest you pick up ANSI CL or PCL, and get a good Emacs+SLIME+Paredit setup. Lisp is best learned interactively - you can just try things out and confirm the behaviour shown in the books and in the (Hyper)spec. I've been using CL for some 6 months now, and I'm pretty much fluent in it, except for MOP and more advanced CLOS trickery, a situation I plan to remedy by reading AMOP. Good luck!

Don't change these.
Name: Email:
Entire Thread Thread List