From: "Frédéric van der Plancke" <fvdp@decis.be>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Combinatorics in a functional way
Date: Wed, 21 Feb 2007 13:17:50 +0100 [thread overview]
Message-ID: <45DC386E.9030403@decis.be> (raw)
In-Reply-To: <53c655920702210236k66c75c37r3a83a0d0c0748de@mail.gmail.com>
David Baelde wrote:
> Hi,
>
> Here's a possibility. It certainly feels more functional, and also
> consumes less memory, but I'm not sure that it fits your needs.
> Basically the idea is to build an iterator over permutations, using a
> function computing the next permutation from a given one -- this is
> basically incrementing a list of digits seen as a number.
>
> I'm lazy so I didn't pass min/max as parameters everywhere. My
> solution doesn't use different upper bounds for digits like you (p0
> for i0 to i2; p1 for i3 to i7) but it could be adapted.
>
> let min = 2
> let max = 4
>
> exception Last
>
> let rec init n = if n=0 then [] else min::(init (n-1))
>
> let rec next acc = function
> | [] -> raise Last
> | x::l ->
> if x<max then List.rev_append acc ((x+1)::l) else next (min::acc) l
>
> (* Iterates [f] over all lists of [len] digits between [min] and
> [max]. *)
> let iter_combi len f =
> let rec iter cur =
> f cur ;
> iter (next [] cur)
> in
> try iter (init len) with Last -> ()
You can also define iter_combi recursively in terms of itself:
(* Iterates [f] over all lists of [len] digits between [min] and [max]. *)
let rec iter_combi min max len f =
assert (len >= 0);
if len = 0 then (f [])
else iter_combi min max (len - 1) (fun tail ->
for x = min to max do
f (x :: tail)
done)
(if you want the lists be passed to f in lexicographical order, write f
(List.rev (x::tail)) instead of f(x::tail).)
Frédéric
>
> let _ =
> iter_combi 3
> (fun c ->
> print_string (String.concat " " (List.map string_of_int c)) ;
> print_newline ())
>
> By the way, I started using this thing when coding in C to avoid the
> memory cost of generating the list of permutations, not because it
> felt more functional.
>
> Hope that helps.
next prev parent reply other threads:[~2007-02-21 12:18 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 [this message]
2007-02-21 11:06 ` Pietro Abate
2007-02-21 13:19 ` Jacques Carette
2007-02-21 11:29 ` Nicolas Pouillard
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=45DC386E.9030403@decis.be \
--to=fvdp@decis.be \
--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