Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Ml > Re: Really weir...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 906 of 968
Post > Topic >>

Re: Really weird function evaluation and errors in SMLNJ

by torbenm@[EMAIL PROTECTED] (Torben Ęgidius Mogensen) Jul 12, 2007 at 08:25 PM

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
 




 1 Posts in Topic:
Re: Really weird function evaluation and errors in SMLNJ
torbenm@[EMAIL PROTECTED]  2007-07-12 20:25:26 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 18:08:34 CDT 2008.