This is the mail archive of the guile@sources.redhat.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Adding infix arithmetic expressions to guile


> 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 



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]