From oleg@pobox.com Wed May 9 18:04:57 2001 Date-Sent: Wed, 9 May 2001 18:04:55 -0700 (PDT) Date: Thu, 10 May 2001 01:00:27 +0000 (UTC) Newsgroups: comp.lang.scheme From: oleg@pobox.com Message-Id: <200105100104.SAA76113@adric.cs.nps.navy.mil> To: comp.lang.scheme@mailgate.org References: <9dag5f$6oas3$1@fido.engr.sgi.com> Subject: Quote as a macro Reply-to: oleg@pobox.com Status: OR Rob Warnock wrote: > After whitespace & comment elimination, I usually consider there to be > nine pieces of "primitive syntax"... quoted literals ... This article shows that the quote is not a primitive construct. It can easily be defined if a Scheme implementation happens to lack it ;) More seriously, the example gives a Scheme implementor a choice to leave out the quote from the core of his Scheme system (his virtual machine) (define-macro my-quote (lambda (body) (let ((s-list (string->symbol "list")) (s-cons (string->symbol "cons")) (s-vector (string->symbol "vector")) (s-my-quote (string->symbol "my-quote")) (s-string->symbol (string->symbol "string->symbol")) ) (cond ((null? body) (list s-list)) ; Expansion: (list) ((list? body) (cons s-list ; expansion: (list (my-quote el1) ...) (map (lambda (el) (list s-my-quote el)) body))) ((pair? body) (list s-cons (list s-my-quote (car body)) (list s-my-quote (cdr body)))) ((vector? body) (cons s-vector ; expansion: (vector (my-quote el1) ...) (map (lambda (el) (list s-my-quote el)) (vector->list body)))) ((symbol? body) (list s-string->symbol (symbol->string body))) (else body))))) ; The following statements print the expected results: (display (append (my-quote (1 2 3)) (my-quote ()) (my-quote (4)))) (newline) (write (my-quote "string")) (newline) (write (my-quote (1 . 2))) (newline) (write (my-quote (1 2 a 4 . 5))) (newline) (write (my-quote #(1 #\2 xyz))) (newline) (write (vector-length (my-quote #(1 2 3)))) (newline) ; the quote inside my-quote loses its meaning, as it should (write (my-quote (1 #(1 2) '4 xyz #t))) (newline) (write (symbol? (my-quote abcdef))) (newline) Tested on Gambit-C 3.0, Bigloo 2.4b, Gauche 0.6.3 and SCM 5d6. On SCM, my prelude myenv-scm.scm, should be loaded first. Macros are darn powerful indeed.