caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Miles Egan <miles@caddr.com>
To: caml-list@inria.fr
Subject: Re: [Caml-list] recursive variants
Date: Sat, 12 May 2001 14:49:43 -0700	[thread overview]
Message-ID: <20010512144943.A59604@caddr.com> (raw)
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

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


      reply	other threads:[~2001-05-12 21:49 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-05-12  5:52 Miles Egan
2001-05-12 15:13 ` [Caml-list] converting a list to a Stream Terrence Brannon
2001-05-12 15:31   ` Sylvain Pogodalla
2001-05-12 15:35   ` Didier Le Botlan
     [not found] ` <3AFCFB23.CB503721@tsc.uc3m.es>
2001-05-12 15:28   ` [Caml-list] recursive variants Miles Egan
2001-05-12 15:35 ` Sven LUTHER
2001-05-12 21:49   ` Miles Egan [this message]

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=20010512144943.A59604@caddr.com \
    --to=miles@caddr.com \
    --cc=caml-list@inria.fr \
    /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).