caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Jonathan Roewen" <jonathan.roewen@gmail.com>
To: OCaml <caml-list@yquem.inria.fr>
Subject: [Caml-list] Trying to understand recursion curiosity
Date: Fri, 31 Mar 2006 15:00:49 +1200	[thread overview]
Message-ID: <ad8cfe7e0603301900p461a05c5xb3961b11f98c2910@mail.gmail.com> (raw)

Hi,

I have some code for implementing some parser combinators, and have
stumbled upon some unexpected behaviour in my definition of a
combinator that's recursive.

Here's the definitions:

> (* some parsers *)
> let x = literal "x"
> let s = literal ","

> (* initial definition *)
> let rec one_or_more_with_sep pa ps =
>     try_with
>        (then3 (fun x y z -> x :: z) pa ps (one_or_more_with_sep pa ps))
>        (pa --> (fun x -> [x]))

> (* try to define a parser *)
> let xs = one_or_more_with_sep x s

Stack overflow during evaluation (looping recursion?).

> (* new definition *)
> let rec one_or_more_with_sep pa ps = fun ts ->
>     try_with
>         (then3 (fun x y z -> x :: z) pa ps (one_or_more_with_sep pa ps))
>         (pa --> (fun x -> [x]))
>         ts

> (* try to define a parser with amended definition *)
> let xs = one_or_more_with_sep x s

val xs : string list -> (string list * string list) list = <fun>

I've omitted the definitions of try_with, then3, and apply (-->).
try_with tries the first parser, and if it returns the empty list,
uses the second.

BTW: a parser returns a list of possible parses.

Kindest Regards,

Jonathan Roewen


                 reply	other threads:[~2006-03-31  3:00 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=ad8cfe7e0603301900p461a05c5xb3961b11f98c2910@mail.gmail.com \
    --to=jonathan.roewen@gmail.com \
    --cc=caml-list@yquem.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).