From: "David Baelde" <david.baelde@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 11:36:03 +0100 [thread overview]
Message-ID: <53c655920702210236k66c75c37r3a83a0d0c0748de@mail.gmail.com> (raw)
In-Reply-To: <20070221203603.e222647a.mle+ocaml@mega-nerd.com>
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 -> ()
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.
--
David
next prev parent reply other threads:[~2007-02-21 10:36 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 ` David Baelde [this message]
2007-02-21 12:17 ` [Caml-list] " 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
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=53c655920702210236k66c75c37r3a83a0d0c0748de@mail.gmail.com \
--to=david.baelde@gmail.com \
--cc=caml-list@yquem.inria.fr \
--cc=david.baelde@ens-lyon.org \
--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