Version française
Home     About     Download     Resources     Contact us    
Browse thread
recursion/iterator question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nils Gesbert <nils.gesbert@e...>
Subject: Re: [Caml-list] recursion/iterator question
Li-Thiao-Té Sébastien <sayan@crans.org> a écrit :

» let iterate f l =
»    let t = Array.of_list l in
»    let n = Array.length t in
»    let results = ref [] in
»    for i = 0 to n-1 do
»      for j = 0 to n-1 do
»        for k = 0 to n-1 do
»          results := f (t.(i),t.(j),t.(k)) :: results
» done done done
» ;;

Hi,

A functional version of this could be (assuming f is curryed, and without keeping results) :
let iter3 f l =
   let rec it2 iter g = function
      h::t -> iter (g h) l; it2 iter g t
    | _ -> ()
   in it2 (it2 List.iter) f l

But I think the goal was to iterate over triplets (a,b,c) with a < b < c, not over all triplets. If I am not mistaken, it suffices to replace the l on line 3 by t to get this behaviour.

--
Nils Gesbert