[racket] specifying strings without escaping backslash
2013/12/5 Laurent <laurent.orseau at gmail.com>:
> Someone ought to build the `at-any-exp' language Matthew was talking about a
> few days ago, so that one can write
> #lang at-any-exp $ racket
> $~a{Yay, dollar-exp syntax!}
Some time ago (the time stamp says april) I experimented with modifying
the readtable for a language. I can't remember exactly how far I got,
at least the following example works.
The first 4 lines are:
1. #lang readtable
2. {} terminating-macro "pratt.rkt" parse
3. #lang racket/base
4. <first line of program in the racket/base language begins>
The 1. lines says that we are using the readtable language.
The 2. lines says that the readtable for {} are changed,
and that parse in "pratt.rkt" should be used to parse
the contents between any {} in the following program.
The 3. line says that the program is written in racket/base
(but we can now use {} with our meaning).
It so happens that the example parse in "pratt.rkt"
implements infix arithmetic expressions, so this example
calculates the roots of a qudratic polynomial.
#lang readtable
{} terminating-macro "pratt.rkt" parse
#lang racket/base
(provide solve)
(define (solve a b c)
(define d {b^2-4*a*c})
(cond [(< d 0) '()]
[(= d 0) (list {-b/(2*a)})]
[else (define r1 {(-b-sqrt[d])/(2*a)})
(define r2 {(-b+sqrt[d])/(2*a)})
(list (min r1 r2) (max r1 r2))]))
(solve 1 0 -4) ; => '(-2 2)
(solve 1 0 0) ; => '(0)
(solve 1 0 1) ; => ()
Note that overloading {} using the readtable is different
from the standard hack of letting #%app expand applications
with {} in a different from how ()-applications are expanded.
Exercise: Replace the formula with one, that doesn't cause
a numerical analyst to have a heart attack.
The mechanisms behind the readtable language can
be used to implement, the
#lang at-any-exp $ racket
syntax.
Code is available here:
https://github.com/soegaard/this-and-that/blob/master/readtable/test2.rkt
/Jens Axel