English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
Re: [Caml-list] Efficiency of let/and
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2005-09-26 (23:25)
From: Oliver Bandel <oliver@f...>
Subject: Re: [Caml-list] Efficiency of let/and
On Sun, Sep 25, 2005 at 08:31:55AM -0500, Brian Hurt wrote:
> Say I have two variables I want to set- variable a to the value expr1 and 
> variable b to the value expr2.  The two expressions are pure (no side 
> effects), and neither one depends upon the other (neither expr1 nor expr2 
> contain either a or b as a value), so they can be evaluated in either 
> order or in parallel with no harm.  With expressions like these, I've 
> gotten into the habit of using let/and to express the parallelism, that is 
> I go:
> 	let a = expr1
> 	and b = expr2
> 	in
> 	...
> rather than:
> 	let a = expr1 in
> 	let b = expr2 in
> So my question is: is there any value (other than the documentation value) 
> in doing this?

When you write it in the second form, then the "let a ="
must be evaluated before the "let b =" expression, because "a"
is used in the "b" expression (or might be used, but "a"
is part of "b"'s environment, so that it must be evaluated

You can use this form also for doing imperative operations in
a certain order, because the more-outer expression is evaluated first.

If you use the form with "and" you can use definitions for functions,
that call each other, so called mutually recursive functions.
(For types this is also possible (e.g. tree-stuff).)

An example (that makes no sense to be used and will raise an
Stack-overflow exception, but I want to show, what is possible to define):

# let rec f1 x = f2 x + 10
  and f2 x = f1 x + 100;;
val f1 : 'a -> int = <fun>
val f2 : 'a -> int = <fun>
# f2 3;;
Stack overflow during evaluation (looping recursion?).

One could try to use "rec" for binding of simple values
(not functions with parameters), but this would make no sense,
and if evaluated would also create an overflow exception.

When defining mutually recursive functions, the recursion would
start, when calling the function with it's arguments, so that
the resulting value would be calculated. E.g. in the toplevel,
you can do this by calling the functions with a parameter
(as done above).

But when you have a simple value, and not a function,
then it's value would be calculated right after the
definition, and so the definition of that value
would rise the exception, because the expression
would be immediately evaluated (eager evaluation).

This would probably yield to difficulties, and that
seems to me to be the reason, why it is forbidden to use such
an expression:

first:~ oliver$ ocaml
        Objective Caml version 3.08.0

# let rec a = 4
   and b = 6;;
val a : int = 4
val b : int = 6
# let rec a = b + 4
  and b = a * 8;;
This kind of expression is not allowed as right-hand side of `let rec'

So, you can use "and" for defining some variables at the same
scope/environment in parallel, that do not depend on each other...
... but if you define functions, you can also define functions,
that depend on each other (mutually recursive calls).

Hope this helps.