caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jacques Carette <carette@mcmaster.ca>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Combinatorics in a functional way
Date: Wed, 21 Feb 2007 08:19:22 -0500	[thread overview]
Message-ID: <45DC46DA.6030201@mcmaster.ca> (raw)
In-Reply-To: <20070221110607.GB15796@pulp.rsise.anu.edu.au>

And with the Seq module (left at end), combined with the pa_monad 
extention, one can rewrite find_comb as

let find_comb p0 p1 =
  perform with Seq in
    i0 <-- range p0;
    i1 <-- range p0;
    i2 <-- range p1;
    guard (test i0 i1 i2);
    return (i0, i1, i2)

I do believe that is more readable :-)

Jacques

Pietro Abate wrote:
> I'm not sure this is exactly what you want... but I think it's a good
> starting point to look at for this kind of problems. Making it lazy is
> just a matter of changing the definition of the modules Seq.
>
> module Seq =
>     struct
>         let mzero = []
>         let return a = [a]
>         let bind m f = List.flatten (List.map f m)
>         let mplus = List.append
>         let guard b = if b then return () else mzero
>     end
>         ;;
>
> let range n =
>   let rec aux i l =
>       if i = 0 then l else i::(aux (i-1) l)
>   in List.rev ( aux n [] )
> ;;
>
> let test _ _ _ = true ;;
>
> let find_comb p0 p1 =
>     Seq.bind (range p0) (fun i0 ->
>         Seq.bind (range p0) (fun i1 ->
>             Seq.bind (range p1) (fun i2 ->
>                 Seq.bind (Seq.guard (test i0 i1 i2)) (fun _ ->
>                     Seq.return (i0,i1,i2)
>                 )
>             )
>         )
>     )
> ;;
>
>   


  reply	other threads:[~2007-02-21 13:19 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 [this message]
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=45DC46DA.6030201@mcmaster.ca \
    --to=carette@mcmaster.ca \
    --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).