Browse thread
Combinatorics in a functional way
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Jacques Carette <carette@m...> |
| Subject: | Re: [Caml-list] Combinatorics in a functional way |
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)
> )
> )
> )
> )
> ;;
>
>