If you want to βoutputβ the results from a lambda function, then the lambda function itself must return a sequence (and therefore, the caller of the lambda function must also return a sequence). Thus, you cannot get what you want without changing the permutations function (because you cannot transfer values ββfrom code running in one (nested) region to a list defined in some other (outer) region).
However, you can change permutations to look something like this:
let permutations f (alphabet:'a array) = let swap ij = let aux = alphabet.[i] alphabet.[i] <- alphabet.[j] alphabet.[j] <- aux let rec permutations' n = seq { if n = alphabet.Length then yield! f alphabet else for i in n..(alphabet.Length-1) do swap ni yield! permutations' (n+1) swap ni } permutations' 0
I wrapped permutations' in a seq { .. } block seq { .. } and added yield! to f alphabet (so that all elements created by f are passed as results), and I also added yield! to recursive call.
Then you can write:
permutations (fun arr -> seq { yield arr |> Array.map id }) [|1;2;3|]
The code uses Array.map id instead of Clone , so you get a copy of the array type, not obj , returned by the .NET cloning mechanism.
However, I suppose you really don't need to output multiple elements from lambda, so you can change yield! f alphabet yield! f alphabet to just yield f alphabet (to return only one element, not multiple elements) and write:
permutations (fun arr -> arr |> Array.map id) [|1;2;3|]
This way you will at least get a good way to abstract cloning behavior (and you can clone or not clone an array easily).
source share