caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Oliver Bandel <oliver@first.in-berlin.de>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Efficiency of let/and
Date: Tue, 27 Sep 2005 01:25:11 +0200	[thread overview]
Message-ID: <20050926232511.GA792@first.in-berlin.de> (raw)

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


             reply	other threads:[~2005-09-26 23:25 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-09-26 23:25 Oliver Bandel [this message]
  -- 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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20050926232511.GA792@first.in-berlin.de \
    --to=oliver@first.in-berlin.de \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).