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 XAA08687; Sat, 12 May 2001 23:49:52 +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 XAA08673 for ; Sat, 12 May 2001 23:49:51 +0200 (MET DST) Received: from www.invert.com (invert.com [209.164.21.15]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f4CLnnb10029 for ; Sat, 12 May 2001 23:49:50 +0200 (MET DST) Received: (from miles@localhost) by www.invert.com (8.10.1/8.10.1AA) id f4CLnhI59685 for caml-list@inria.fr; Sat, 12 May 2001 14:49:43 -0700 (PDT) (envelope-from miles) Date: Sat, 12 May 2001 14:49:43 -0700 From: Miles Egan To: caml-list@inria.fr Subject: Re: [Caml-list] recursive variants Message-ID: <20010512144943.A59604@caddr.com> References: <20010511225250.A50306@caddr.com> <20010512173553.A27913@lambda.u-strasbg.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <20010512173553.A27913@lambda.u-strasbg.fr>; from luther@dpt-info.u-strasbg.fr on Sat, May 12, 2001 at 05:35:53PM +0200 Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk On Sat, May 12, 2001 at 05:35:53PM +0200, Sven LUTHER wrote: > type word = string > type wordlist = string list > type phrase = unit -> wordlist > type phraselist = phrase list > > and then : > > type grammar_element = > | Word of word > | Wordlist of wordlist > | Phrase of phrase > | Phraselist of phraselist I finally got this to work: (*****************************************************) type grammar_element = Word of string | Term of (unit -> string) | Nonterm of (unit -> grammar_element list) | Terms of grammar_element list let adj () = "big" let prep () = "to" let article () = "the" let noun () = "man" let verb () = "hit" let noun_phrase () = [Term article; Term noun] let verb_phrase () = [Term verb; Nonterm noun_phrase] let sentence () = [Nonterm noun_phrase; Nonterm verb_phrase] let rec generate part = match part with Terms p -> Terms (List.map generate p) | Nonterm p -> generate (Terms (p ())) | Term p -> Word (p ()) | Word p -> Word p let _ = let s = generate (Nonterm sentence) in let rec printelem e = match e with Word w -> print_string (w ^ " ") | Term t -> print_string "term " | Nonterm t -> print_string "nonterm " | Terms t -> List.iter printelem t in match s with Terms t -> List.iter printelem t (*****************************************************) The trick was making sure that generate is of type (grammar_element -> grammar_element) and defining a grammar_element such that a grammar_element list is also a grammar_element list. This seems a bit cumbersome. Is there a cleaner way to do this? How do people generally handle heterogeneous lists in ocaml? -- miles ------------------- To unsubscribe, mail caml-list-request@inria.fr. Archives: http://caml.inria.fr