Browse thread
recursion/iterator question
[
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: | David Powers <david@g...> |
| Subject: | Re: [Caml-list] recursion/iterator question |
The best way would probably be to write a permutation function that
returns a list of triplets given a list, and then to use List.iter and a
local function to split out each triplet to work on the component
parts... something like:
let for_each_trip lst =
let real_work (a, b, c) =
(* do some stuff with a b and c in here *)
in
List.iter real_work (permute_triples lst)
(loose code - forgive my laziness)
This could be made more general by improving the permute code to take a
number option and to return triplets of that number (permute 3 lst).
In addition you could make the internal function (real_work) be a
parameter of the for_each... which would reduce all of this to the more
general
List.iter any_function_I_want (permute 3 lst)
Finally, you might want to consider using lazy evaluation in your
permute function to avoid calculating every permutation up front. See
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Lazy.html for more on
that.
-David
Tato Thetza wrote:
> Hi caml-list
> Given a list, I would like to iterate over all triplets in the list. For
> example, in mathematcs, its not uncommon to have expressions such as
> "for all i,j,k in set X, do f(i,j,k)"
>
> The only way I can think of is to create a list with all triplets of the
> list, so:
> triplets([1,2,3,4]) = [(1,2,3),(1,2,4),(1,3,4),(2,3,4)]
> and take this list and map a function f to it.
>
> questions:
> 1) what would be the best way to write triplets?
> 2) is there a cleaner way to iterate over all triplets in a list?
>
> please excuse my english
> Tato T.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>