From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id WAA12210; Tue, 20 Jan 2004 22:58:07 +0100 (MET) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id WAA12018 for ; Tue, 20 Jan 2004 22:58:05 +0100 (MET) Received: from herd.plethora.net (herd.plethora.net [205.166.146.1]) by nez-perce.inria.fr (8.11.1/8.11.1) with ESMTP id i0KLw4v01063 for ; Tue, 20 Jan 2004 22:58:04 +0100 (MET) Received: from bhurt.plethora.net (bhurt.plethora.net [205.166.146.49]) by herd.plethora.net (8.11.6/8.10.1) with ESMTP id i0KLvUZ22321; Tue, 20 Jan 2004 15:57:33 -0600 (CST) Date: Tue, 20 Jan 2004 17:00:12 -0600 (CST) From: Brian Hurt X-X-Sender: bhurt@localhost.localdomain To: Michal Moskal cc: erayo@cs.bilkent.edu.tr, Ocaml Mailing List Subject: Re: [Caml-list] ANNOUNCE: mod_caml 1.0.6 - includes security patch In-Reply-To: <20040120173423.GA19476@roke.freak> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 michal:01 moskal:01 haskell:01 recursion:01 inlining:01 poking:01 unmanageable:01 compiler:01 compiler:01 ocaml:01 ocaml:01 caml:01 imho:01 assembler:01 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk 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