[gambit-list] dealing with \
Marc Feeley
feeley at iro.umontreal.ca
Sun Jan 25 18:40:10 EST 2009
On 25-Jan-09, at 5:31 PM, symbolic expression wrote:
> is there any option I can pass to gsi so that the following code
> will work?
>
> > (pp '(\ 1 2 3))
> *** ERROR IN (console)@1.11 -- Invalid infix syntax
> > 3
> > *** ERROR IN (console)@1.14 -- Datum or EOF expected
> > *** ERROR IN (console)@1.15 -- Datum or EOF expected
>
> thanks!
The behavior of read and write (i.e. the mapping from the external
representation to the internal object representation) is controlled
with a "readtable" which is a structure storing various settings. By
default the main readtable (i.e. ##main-readtable) supports a number
of Scheme extensions, and in particular the SIX syntax (Scheme Infix
eXtension). The SIX syntax is explained here:
http://www.iro.umontreal.ca/~gambit/doc/gambit-c.html#SEC126
Basically the backslash character announces a switch from the usual
prefix syntax to the infix syntax (which is very much like the C
syntax).
This behavior can be changed by setting a new handler for the
backslash character in the main readtable. Below is an example. Note
that the readtable also affects the way in which objects are written.
;; Change the way the backslash character is handled by read and write.
(##readtable-char-class-set!
##main-readtable ;; change the main readtable
#\\ ;; char to dispatch on
#f ;; *NOT* a delimiter
##read-number/keyword/symbol) ;; handler (normally set to ##read-six)
(define x (read (open-input-string "(\\ 1 2 3)")))
(write x) ;; will write: (\ 1 2 3)
(newline)
;; Set the readtable back to the default behavior.
(##readtable-char-class-set!
##main-readtable
#\\
#t
##read-six)
(write x) ;; will write: (|\\| 1 2 3)
(newline)
(define y (read (open-input-string "(\\ 1 ; 2 3)")))
(write y) ;; will write: ((six.literal 1) 2 3)
(newline)
You can put the call to ##readtable-char-class-set! in various
places. If you want all your invocations of gsi and gsc to behave
this way, then put the call in ~/.gambcini.scm .
Then you can do things like:
(define-macro (\ . rest) `(lambda , at rest))
(pp (map (\ (x) (* x x)) '(1 2 3))) ;; prints: (1 4 9)
Of course it might be cuter to use the Unicode lambda like this:
(define-macro (λ . rest) `(lambda , at rest))
(pp (map (λ (x) (* x x)) '(1 2 3))) ;; prints: (1 4 9)
and select an appropriate Unicode character encoding for file I/O,
such as UTF-8:
% gsi -:f8 test.scm
Marc
More information about the Gambit-list
mailing list