[clean-list] Passing an environment around
Pieter Koopman
pieter@cs.kun.nl
Thu, 19 Oct 2000 10:42:05 +0200
Hello Romildo and clean-list
At 05:12 18/10/00 -0200, Jos=E9 Romildo Malaquias wrote:
>Hello.
>
>I am implementing a Computer Algebra system (CALG) in Clean, and I have a
>problem I would like the opinion of Clean programmers.
...[snip]...
You can use the curried functions to implement infix operator to implement=
=20
infix operators with 3 (or more) arguments.
An example from the standard environment is:
(o) infixr 9
(o) f g :=3D=3D \ x -> f (g x)
A more elaborated example is a very simple evaluator for expressions=20
constisting of values and names:
------------------
module calg
import StdEnv
:: Memory :=3D=3D [(Name,Value)]
read :: Name Memory -> Value
read name [] =3D abort ("No value for "+++toString name)
read name [(n,v):r]
| n=3D=3Dname
=3D v
=3D read name r
:: Operator :=3D=3D Memory -> Value
//name :: Name -> Operator
name :=3D=3D read
value :: Value -> Operator
value c =3D \mem -> c
(.+.) infixl 6 :: !Operator !Operator -> Operator
(.+.) e1 e2 =3D \mem -> e1 mem + e2 mem
(.*.) infixl 7 :: !Operator !Operator -> Operator
(.*.) e1 e2 =3D \mem -> e1 mem * e2 mem
:: Name :=3D=3D String
:: Value :=3D=3D Int
f :: Operator Operator -> Operator
f x y =3D x .+. y .+. y
Start =3D (value 2 .*. f (value 7) (value 1 .+. name "y" .*. name "z"))=
amemory
where
amemory =3D [("x",1),("y",2),("z",3)]
---------------------
If you want to prevent the double evaluation of the second argument of the=
=20
function f you can replace the function by:
fopt :: Operator Operator -> Operator
fopt x y =3D \mem -> let vx =3D x mem; vy =3D y mem in vx + vy + vy
Does this help?
Have fun, Pieter Koopman