John Terrence <nowhere@[EMAIL PROTECTED]
> writes:
> Dear all --
>
> I'm reading the book "Elements of Functional Programming", by Chris
> Reade.
>
> At this point, the example is about converting an imperative loop (p.
63):
>
> Prog: BEGIN Z := 1
> WHILE X <= Y DO
> BEGIN
> Z := Z * X
> X := X + 1
> END
> END
>
> to functional style. This would be:
>
> fun f1 (x,y,z) = (x,y,1)
> fun f2 (x,y,z) = (x,y,z * x)
> fun f3 (x + 1, y, z) = (x + 1, y, z)
> val body = f3.f2
>
> The last line is the notation for functional composition, right? Is it
> correct to use a dot?
No. A dot is used for qualified names (module plus name), such as
Int.toString. Since there is no module named f3 with an indentifier
named f2, you get an error. Use o for function composition. (Haskell
uses . for function composition, though).
Also, x+1 is not a valid pattern, as + is not a constructor. Again,
you might think of Haskell, which allows n+k patterns. Even if ML did
allow these, your f3 would just be the identity function.
In normal ML-style, the imperative loop would be written like:
fun f (z,x) =
if x<=y
then f(z*x, x+1)
else (z,x)
val (z,x) = f(1,x)
This assumes x and y are already defined, as y is used in the body of
f and x is used in the initial call to f. Note that there are three
different variables called x: The previously defined variable, the
parameter to f and the x defined in the last line.
Torben


|