caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Re: [Caml-list] Efficiency of let/and
@ 2005-09-26 23:25 Oliver Bandel
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Bandel @ 2005-09-26 23:25 UTC (permalink / raw)
  To: caml-list

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
first).

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.

Ciao,
   Oliver


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] Efficiency of let/and
  2005-09-25 13:31 Brian Hurt
@ 2005-09-25 14:47 ` Jon Harrop
  0 siblings, 0 replies; 2+ messages in thread
From: Jon Harrop @ 2005-09-25 14:47 UTC (permalink / raw)
  To: caml-list

On Sunday 25 September 2005 14:31, Brian Hurt wrote:
> So my question is: is there any value (other than the documentation value)
> in doing this?

Good question. I've no idea. However, I did notice that this change has been 
undone (i.e. to use "let" twice and not "and") in the stdlib.

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
Objective CAML for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2005-09-26 23:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-09-26 23:25 [Caml-list] Efficiency of let/and Oliver Bandel
  -- strict thread matches above, loose matches on Subject: below --
2005-09-25 13:31 Brian Hurt
2005-09-25 14:47 ` [Caml-list] " Jon Harrop

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).