caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Nicolas Pouillard" <nicolas.pouillard@gmail.com>
To: "Erik de Castro Lopo" <mle+ocaml@mega-nerd.com>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Combinatorics in a functional way
Date: Wed, 21 Feb 2007 12:29:06 +0100	[thread overview]
Message-ID: <cd67f63a0702210329v2357c938yd0f47fe218e3297c@mail.gmail.com> (raw)
In-Reply-To: <20070221203603.e222647a.mle+ocaml@mega-nerd.com>

On 2/21/07, Erik de Castro Lopo <mle+ocaml@mega-nerd.com> wrote:
> Hi all,

Hi,

[...]

>
> Can anyone come up with a cleaner, more functional way of solving
> problems like this? I'm thinking that something like lazy lists
> might be a solution.
>

I need it some days ago and came to two different solutions.

In my case I needed to iterate over list permutations.

So in your case just use a function like range:

let range n =
 let rec aux i l =
     if i = 0 then l else i::(aux (i-1) l)
 in List.rev ( aux n [] )
;;

1/ The first is short but consume memory:

let fold = List.fold_right;;

let cartesian_product llz =
  fold begin fun ly llx ->
    fold begin fun y acc ->
      fold begin fun lx acc ->
        (y :: lx) :: acc
      end llx acc
    end ly []
  end llz [[]]
;;

cartesian_product [range p0; range p1; range p2];;

2/ This one consider list given as input like a counting base
(decimal, hexadecimal...).

type 'a enum = Stop | Elt of (('a list) * (unit -> 'a enum))

let enum_cartesian_product ll =
  let dupl l =
    List.rev_map begin fun x ->
      if x = [] then invalid_arg "enum_cartesian_product: empty list";
      (x, x)
    end l in
  let rec wrap_result res =
    let element = List.map (fun (x, _) -> List.hd x) res
    and kont () = increment (List.rev res) true []
    in (element, kont)
  and increment revll ret res =
    match revll with
    | [] ->
        if ret then (* it is the real end of the stream *) Stop
        else (* that's a element of stream *) Elt(wrap_result res)
    | (l, init) :: rest ->
        if ret then begin
          match l with
          | [] -> assert false
          | [ v ] -> increment rest true ((init, init) :: res)
          | v :: other -> increment rest false ((other, init) :: res)
        end else increment rest false ((l, init) :: res)
  in Elt(wrap_result (dupl ll))

let rec iter f =
  function
  | Elt(v, kont) -> f v; iter f (kont ())
  | Stop -> ()
;;

let e = enum_cartesian_product [range p0; range p1; range p2]
in
iter begin fun combi ->
  print_endline (String.concat "; " (List.map string_of_int combi))
end e;;

Hope that helps,

-- 
Nicolas Pouillard


  parent reply	other threads:[~2007-02-21 11:29 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-02-21  9:36 Erik de Castro Lopo
2007-02-21 10:36 ` [Caml-list] " David Baelde
2007-02-21 12:17   ` Frédéric van der Plancke
2007-02-21 11:06 ` Pietro Abate
2007-02-21 13:19   ` Jacques Carette
2007-02-21 11:29 ` Nicolas Pouillard [this message]
2007-02-21 12:24   ` Gabriel Kerneis
2007-02-21 13:46 ` Fernando Alegre
2007-02-21 14:36   ` Brian Hurt
2007-02-22 10:01     ` Erik de Castro Lopo

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=cd67f63a0702210329v2357c938yd0f47fe218e3297c@mail.gmail.com \
    --to=nicolas.pouillard@gmail.com \
    --cc=caml-list@yquem.inria.fr \
    --cc=mle+ocaml@mega-nerd.com \
    /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).