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 UAA16568; Wed, 28 Apr 2004 20:46:12 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id UAA16557 for ; Wed, 28 Apr 2004 20:46:11 +0200 (MET DST) Received: from ptb-relay03.plus.net (ptb-relay03.plus.net [212.159.14.214]) by concorde.inria.fr (8.12.10/8.12.10) with ESMTP id i3SIkAYM013244 for ; Wed, 28 Apr 2004 20:46:10 +0200 Received: from [80.229.56.224] (helo=chetara) by ptb-relay03.plus.net with esmtp (Exim) id 1BIu4P-00007R-QT for caml-list@inria.fr; Wed, 28 Apr 2004 18:46:09 +0000 From: Jon Harrop Organization: University of Cambridge To: caml-list Subject: Re: [Caml-list] [ANN] The Missing Library Date: Wed, 28 Apr 2004 19:42:05 +0100 User-Agent: KMail/1.5.4 References: <20040428151818.GC1310@excelhustler.com> <1083169714.9537.1102.camel@pelican.wigram> In-Reply-To: <1083169714.9537.1102.camel@pelican.wigram> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200404281942.05371.jdh30@cam.ac.uk> X-Miltered: at concorde by Joe's j-chkmail ("http://j-chkmail.ensmp.fr")! X-Loop: caml-list@inria.fr X-Spam: no; 0.00; caml-list:01 2004:99 advocating:01 gotos:01 flags:01 bindings:01 bindings:01 ocaml:01 command:98 rec:01 side-effects:02 match:02 tree:02 commands:97 stack:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Wednesday 28 April 2004 5:28 pm, skaller wrote: [snip] > let count = ref 0 in > List.iter > (fun x -> > if !start = 0 then > (start := 1; print "["; print x; ) > else (print ";"; print x) > ) > lst; > if (!start <> 0) then print "]" You mean: let print_list string_of l = print_string ("["^(String.concat "; " (List.map string_of l))^"]") Personally I'd factor out the making of and printing of the string. If you want a more "direct" conversion then just write a "list_mapi" function and test "i" each time. > and please don't tell me that isn't a total mess compared with > ... Oh my God, you're not seriously advocating a page of gotos over that are you? > Notice there are NO FLAGS. The entire state > is represented by the program counter. > In a more complex problem, the entire > state can be local. You can even write > a recursive procedure .. that builds a list .. > or even a tree .. do you want modify > the external state like that, building > a data structure to *emulate* a stack > when you can actually have a real one? I think you can easily have it either way in ocaml too. For the imperative style you can use map or iter with a function which has side-effects on some more global state. For the functional approach you use fold and pass the state on each time. For example, I wrote this piece of code yesterday: let rec eval bindings t = match t with ... | Sequence l -> let f (bindings, l) t = let (bindings, t) = eval bindings t in (bindings, t::l) in let (bindings, l) = List.fold_left f (bindings, []) l in (bindings, Sequence (List.rev l)) | ... It loops through a sequence of commands, executing each one in turn which may affect the current state "bindings" and the new state is passed on to the next command. Cheers, Jon. ------------------- 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