[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Chicken-hackers] Enum macro proposal

From: Peter Bex
Subject: [Chicken-hackers] Enum macro proposal
Date: Sun, 28 Oct 2007 14:16:02 +0100
User-agent: Mutt/

Hi all,

For a project I'm currently working on I'm implementing a network
protocol in pure Scheme.  This protocol uses numeric constants as
message types.  Since Chicken is targeted very much at interoperability
with existing C code, I thought the following macro would make a nice
addition to chicken-more-macros.scm:

;; C-like enumerator macro.  Starts counting at 0 and it is possible to
;; change the counter at arbitrary points in the enum.
;; Example:
;; (define-enum foo bar qux)     -> (define foo 0) (define bar 1) (define qux 2)
;; (define-enum foo (bar 3) qux) -> (define foo 0) (define bar 3) (define qux 4)
(define-macro (define-enum . args)
  (let loop ((output '())
             (value 0)
             (args args))
     ((null? args) (cons 'begin output))
     ((pair? (car args))
      (loop (cons `(define ,(caar args) ,(cadar args)) output)
            (add1 (cadar args)) (cdr args)))
      (loop (cons `(define ,(car args) ,value) output)
            (add1 value) (cdr args))))))

It seems silly to create an egg for it and it seems useful enough to have
available from base.  What do you guys think?

"The process of preparing programs for a digital computer
 is especially attractive, not only because it can be economically
 and scientifically rewarding, but also because it can be an aesthetic
 experience much like composing poetry or music."
                                                        -- Donald Knuth

Attachment: pgpM2lDxesiTi.pgp
Description: PGP signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]