This is the mail archive of the
guile@sources.redhat.com
mailing list for the Guile project.
Adding infix arithmetic expressions to guile
- To: Jorgen 'forcer' Schaefer <forcer at mindless dot com>
- Subject: Adding infix arithmetic expressions to guile
- From: Ian Grant <Ian dot Grant at cl dot cam dot ac dot uk>
- Date: Thu, 13 Jul 2000 08:27:04 +0100
- cc: guile at sourceware dot cygnus dot com, Ian dot Grant at cl dot cam dot ac dot uk
> What you say is that it's inconvient for users to use prefix
> syntax for mathematical expressions. The only thing required is
> a special form which handles infix syntax, or maybe even just a
> function like Tcl's expr.
>
> That is, either something like
> (define a 3)
> [syntax] (expr (1+1) * a)
> -> (* (+ 1 1) a)
> => 6
> or
> [function] (expr "(1+1)*a")
> => 6
>
> Actually, the first is just a matter of
>
> (define-syntax expr
> (syntax-rules ()
> ((_ expressions ...)
> (expr*
> (with-output-to-string
> (lambda ()
> (write '(expressions ...)))))))
>
> If you have expr* as the function which evaluates a string.
>
> expr* has to be able to get the value of variables.
>
> It would of course be nicer if this would just be syntax that
> rewrites the expression, but that's not easy (considering that
> parsing (expr 1+1) in define-syntax isn't trivial...)
I've pretty much just written exactly this. For example I can now go:
guile> (use-modules (syntax arith))
guile> (define (magnitude x y) #[sqrt(x ^ 2 + y ^ 2)])
guile> (magnitude 1 1)
1.4142135623731
guile> (procedure-source magnitude)
(lambda (x y) (sqrt (+ (expt x 2) (expt y 2))))
It uses lex and yacc wrapped in a module. I want to do some more work on it but in the mean time if anyone is interested in the source then mail me.
Ian
--
Ian Grant, Computer Lab., New Museums Site, Pembroke Street, Cambridge
Phone: +44 1223 334420 Personal e-mail: iang at pobox dot com