Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Macro criticism

37 views
Skip to first unread message

Robert Klemme

unread,
Aug 23, 2011, 1:15:32 PM8/23/11
to

All,

finally I got around to investing some time to really learn Lisp. I
picked Scheme mostly because of "Structure and Interpretation of
Computer Programs". Since I always wondered how macros in Lisp work, I
As an exercise I implemented my version of "cond" and would like to hear
criticism from more experienced people with regard to what I could have
made better, what might be inefficient or un-idiomatic.

(define (rk:cond->if conditions)
(if (null? conditions)
(error "empty list of conditions")
(let ((first (car conditions))
(remaining (cdr conditions)))
(let ((cond (car first)) ;; this condition
(cmds (cdr first)) ;; commands for this condition
(last (null? remaining))) ;; flag for last entry
(if (eq? cond 'else)
(if last
(cons begin cmds)
(error "else not at end!"))
(cons if (cons cond (cons (cons begin cmds)
(if last
'()
(list (rk:cond->if remaining)))))))))))

(define-macro (rk:cond . conditions)
(rk:cond->if conditions))

So far it seems to work exactly like "cond" (apart from different error
messages :-)).

Thanks a lot!

Kind regards

robert


PS: I case anybody wonders, I use guile as Scheme implementation.

Jason Crupper

unread,
Sep 19, 2011, 3:42:31 AM9/19/11
to
You could have used quasiquotation to make your macro definition
clearer.

(define (rk:cond->if conditions)
   (if (null? conditions)
       (error "empty list of conditions")
       (let ((first (car conditions))
            (remaining (cdr conditions)))
        (let ((cond (car first))        ;; this condition
              (cmds (cdr first))        ;; commands for this
condition
              (last (null? remaining))) ;; flag for last entry
          (if (eq? cond 'else)
              (if last
                  (cons begin cmds)
                  (error "else not at end!"))
              `(if ,cond (begin ,@cmds)
                  ,@(if last
                        '()
                        `(,(rk:cond->if remaining)))))))))

Robert Klemme

unread,
Sep 23, 2011, 3:25:55 PM9/23/11
to
On 19.09.2011 09:42, Jason Crupper wrote:

> You could have used quasiquotation to make your macro definition
> clearer.

Jason, thank you! I'll dive a bit deeper into all those different
quotings and unquotings... :-)

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
0 new messages