caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Michal Moskal <malekith@pld-linux.org>
Cc: erayo@cs.bilkent.edu.tr, Ocaml Mailing List <caml-list@inria.fr>
Subject: Re: [Caml-list] ANNOUNCE: mod_caml 1.0.6 - includes security patch
Date: Tue, 20 Jan 2004 17:00:12 -0600 (CST)	[thread overview]
Message-ID: <Pine.LNX.4.44.0401201641080.4373-100000@localhost.localdomain> (raw)
In-Reply-To: <20040120173423.GA19476@roke.freak>

On Tue, 20 Jan 2004, Michal Moskal wrote:

> Haskell is lazy, ocaml is strict. Consider following snippet of
> ``ocaml'':
> 
> let _ = f (x)
>   where x = g ()
> 
> Now, the reader of the code might take false impression that f() is
> executed before g(). Of course there is no such danger with function
> definitions in where blocks, but still I think readability is the reason
> it is absent from ocaml.
> 

That peice of code would be equivelent to:
let _ =
    let x = g () in
    f x
;;

So precision isn't lost, just hidden.

I would think the following might be more of a problem:

let x = ... ;;

let _ = f x 
(* at this point, does the compiler know which x to use? *)
where x = g ()
;;

Even more hideous possibilities arise:

let rec f x = 
    let x = f (x + 3) 
    in 
    f (2 * x) 
    where f x = 2 + (f x) 
    and x = 3
;;

I admit that I can't follow which fs and which xs are which, and what the
result of that function would be (it'd be typed int -> int, that is
clear).  Even a simple question like "does f 3 even terminate?" is not at 
all obvious, let alone what answer it'd return.

Rewritting it makes it more obvious:

let rec f x =
    let f x = 2 + (f x)
    and x = 3
    in
    let x = f (x + 3) in
    f (2 * x)
;;

Hmm.  Opps- I do have an infinite recursion, don't I?

The solution to the five-deep let clauses is to cut them out to be their 
own top-level functions.  Function calls aren't that expensive.  And Ocaml 
is very good (IMHO maybe even a little too aggressive) about inlining 
functions where it's worthwhile.  And doesn't necessarily inline inner 
functions automatically either.  In poking around in the assembler output 
of various ocaml code I've written, the compiler will often take a 
complicated inner function and compile it like a stand-alone function.
Function calls simply aren't that expensive.

But the point here is that you're not gaining anything by using inner 
functions vr.s outer functions.  If you've got a big, unmanageable 
function, hack it apart into smaller stand-alone subfunctions.

-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners


  parent reply	other threads:[~2004-01-20 21:58 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-01-15 14:03 Richard Jones
     [not found] ` <4006AC01.F2AD2741@decis.be>
2004-01-15 15:42   ` Richard Jones
2004-01-15 16:19     ` Markus Mottl
2004-01-15 16:53       ` Richard Jones
2004-01-16  6:15         ` james woodyatt
2004-01-16  9:34           ` Richard Jones
2004-01-16 19:05             ` Brian Hurt
2004-01-16 18:52               ` Yutaka OIWA
2004-01-16 19:20                 ` Markus Mottl
2004-01-16 19:01               ` Markus Mottl
2004-01-19 10:13               ` Luc Maranget
2004-01-19 11:36                 ` Richard Jones
2004-01-19 14:43                   ` Luc Maranget
2004-01-19 16:10                     ` Richard Jones
2004-01-19 17:46                       ` Markus Mottl
2004-01-19 18:05                         ` Richard Jones
2004-01-19 21:45                           ` Eray Ozkural
2004-01-20 11:31                             ` Markus Mottl
2004-01-20 12:30                               ` Eray Ozkural
2004-01-21 14:01                               ` skaller
2004-01-20 17:34                             ` Michal Moskal
2004-01-20 17:52                               ` Eray Ozkural
2004-01-20 18:54                                 ` Michal Moskal
2004-01-20 19:21                                   ` Markus Mottl
2004-01-20 19:37                                   ` David Brown
2004-01-20 20:38                                     ` Eray Ozkural
2004-01-21 19:07                                     ` Max Kirillov
     [not found]                                       ` <Pine.GSO.4.53.0401211150520.10508@cascade.cs.ubc.ca>
2004-01-22  2:15                                         ` Max Kirillov
2004-01-20 23:00                               ` Brian Hurt [this message]
2004-01-20 23:48                                 ` Eray Ozkural
2004-01-21  0:34                                   ` David Brown
2004-01-21  2:32                                     ` Eray Ozkural
2004-01-21  2:34                                     ` Eray Ozkural
2004-01-21  2:34                                       ` Shawn Wagner
2004-01-21  9:43                                     ` Andreas Rossberg
2004-01-21  5:16                                   ` Brian Hurt
2004-01-19 21:59                           ` Kenneth Knowles
2004-01-19 18:18                         ` David Brown
2004-01-19 19:15                           ` Markus Mottl
2004-01-19 19:19                             ` David Brown
     [not found]                       ` <20040119185746.A12690@beaune.inria.fr>
2004-01-19 18:07                         ` Richard Jones
2004-01-20  1:29                 ` skaller

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=Pine.LNX.4.44.0401201641080.4373-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=erayo@cs.bilkent.edu.tr \
    --cc=malekith@pld-linux.org \
    /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).