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 VAA22615; Tue, 22 Apr 2003 21:39:38 +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 VAA22232 for ; Tue, 22 Apr 2003 21:39:36 +0200 (MET DST) Received: from epexch01.qlogic.org ([63.170.40.3]) by concorde.inria.fr (8.11.1/8.11.1) with ESMTP id h3MJdZv15855 for ; Tue, 22 Apr 2003 21:39:36 +0200 (MET DST) Received: from epmailtmp.qlogic.org ([10.20.33.254]) by epexch01.qlogic.org with Microsoft SMTPSVC(5.0.2195.5329); Tue, 22 Apr 2003 14:39:08 -0500 Received: from [10.20.33.146] ([10.20.33.146]) by epmailtmp.qlogic.org with Microsoft SMTPSVC(5.0.2195.4905); Tue, 22 Apr 2003 14:39:08 -0500 Date: Tue, 22 Apr 2003 14:46:26 -0500 (CDT) From: Brian Hurt X-X-Sender: Reply-To: Brian Hurt To: Ryan Bastic cc: Subject: Re: [Caml-list] Clarification of original post [still having problems] In-Reply-To: <3ea3174b.37b7.27531@gis.net> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-OriginalArrivalTime: 22 Apr 2003 19:39:08.0591 (UTC) FILETIME=[D777C3F0:01C30906] X-Spam: no; 0.00; qlogic:01 caml-list:01 newbie:01 confuses:01 experimented:01 figuring:01 recursively:01 val:01 arrays:01 semantics:01 ocaml:01 int:01 rec:01 signatures:02 failing:02 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sun, 20 Apr 2003, Ryan Bastic wrote: > > Can anyone take some time and help a struggling newbie out? :-( > > This stuff is still failing to parse right for my brain. > > ----- Original Message Follows ----- > > Hello all, > > A few weeks ago I posted a message asking how to create a program in > > OCaml to shuffle vector of strings into an array of arrays. Had some > > problems understanding how to do it in Ocaml properly, and a kind soul > > posted a very elegant solution to the problem :-) unfortunately, i > > still can't understand some things from it. > > > > What follows is some code with comments on how it is expected to be > > used, and also > > where my confusion in the semantics of the code lay. > > > > (* Return first 'n' from input and the rest. *) > > let firstN n input = > > let nInput = Array.length input in > > if n >= nInput > > then (input, [||]) > > else (Array.sub input 0 n, Array.sub input n (nInput - n)) > > > > let group n input = > > let rec group' n input = > > if Array.length input = 0 then [] > > else > > let (front, rest) = firstN n input in > > (* the next line confuses me. i'm aware of :: being a list > > concatenation operator, but in this case, shouldn't group 'n rest be > > returning an array, because that's what firstN returns. I've > experimented in > > the REPL and had no luck in figuring it out. *) > > front :: group' n rest > > in > > Array.of_list (group' n input) > > > > The specific problems that confuse me is that > > let rec group' ... in Array.of_list is all part of the same expression, > so how does Ocaml > let front :: group' n rest work?? firstN n input returns two arrays, and > as far as my > understanding goes, :: is a list concatenation operator. How then, does > this work? You can have lists of arrays just fine. Which is what this function returns. Basically, it (recursively) calls group', which returns a list of arrays. When the recursive call returns, it prepends front to the list and returns the prepended-to list. One trick I find myself doing a lot is to cut internal functions out into global functions to check their type signatures. And given: let rec group' n input = if Array.length input = 0 then [] else let (front, rest) = firstN n input in front :: group' n rest Ocaml returns the type: val group' : int -> 'a array -> 'a array list = Which means a function that takes two arguments, an int and an 'a array, and returns a 'a array list. group' returns a list- so prepending an 'a to the list makes perfect sense. Does this help? 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