This operation is not well-typed, but here's a sample that works on IEnumerable s and returns a list yield! Coalesce(s, dim-1) | _ -> failwith "bad shape" printfn "%A" (coalesce(1;2, 1)) printfn "%A" (coalesce(1;2;3;4, 2)) printfn "%A" (coalesce(1;2;3;4;5;6;7, 3)) You can also write let rec flatten(list:System.Collections. IEnumerable) = for x in list do match x with | :?System.Collections.
IEnumerable as s -> yield! Flatten(s) | _ -> yield x which is more general, e. G let weird : obj list = box 1;2; box 3; 4; box 5;6; box 7 printfn "%A" (flatten weird) EDIT Jon Harrop suggested another strategy - create a new type for nested lists: type NestedListElement = //' | L of NestedListElement list //' | V of 'T //' let rec flatten nlist = for x in nlist do match x with | L l -> yield!
Flatten l | V v -> yield v let nested = LLV 1;V 2; V 3; V 4; LLV 5;V 6; V 7 printfn "%A" (flatten nested).
This operation is not well-typed, but here's a sample that works on IEnumerables and returns a list: let rec coalesce(list:System.Collections. IEnumerable, dim) = if dim=1 then for x in list do yield x else for x in list do match x with | :? System.Collections.
IEnumerable as s -> yield! Coalesce(s, dim-1) | _ -> failwith "bad shape" printfn "%A" (coalesce(1;2, 1)) printfn "%A" (coalesce(1;2;3;4, 2)) printfn "%A" (coalesce(1;2;3;4;5;6;7, 3)) You can also write let rec flatten(list:System.Collections. IEnumerable) = for x in list do match x with | :?System.Collections.
IEnumerable as s -> yield! Flatten(s) | _ -> yield x which is more general, e.g. Let weird : obj list = box 1;2; box 3; 4; box 5;6; box 7 printfn "%A" (flatten weird) EDIT @Jon Harrop suggested another strategy - create a new type for nested lists: type NestedListElement = //' | L of NestedListElement list //' | V of 'T //' let rec flatten nlist = for x in nlist do match x with | L l -> yield! Flatten l | V v -> yield v let nested = LLV 1;V 2; V 3; V 4; LLV 5;V 6; V 7 printfn "%A" (flatten nested).
This operation is not well-typed". In F# but other languages don't have a problem, of course. – Jon Harrop Jul 8 '10 at 22:43.
Sorry, I've a bit knowledge of F# syntax, this is solution in C#, may be it helps: namespace FlatEnumerable { class Program { static void Main(string args) { var arr = new int,, { { { 1, 2 }, { 3, 4 } }, { { 5, 6 }, { 7, 8 } } }; foreach (var I in arr.Flat()) Console. WriteLine(i); } } static class Enumerable2 { public static IEnumerable Flat(this IEnumerable source) { foreach (var item in source) { var enu = item as IEnumerable; if (enu! = null) foreach (var c in enu.Flat()) yield return c; else yield return item; } } } } I belive it can be improved in F# by using pattern matching instead of casting and checking for null.
The F# type system cannot express this. The most common solution is to create a new type representing nested lists.
No, the problem is that coalesce takes a list of lists and the compiler doesn't know that List. Collect(fun item -> item) list always returns a list of lists (as a matter of fact the compiler can't know that because it's not true). However since that's what you pass in as the argument to coalesce the compiler would need to know that in order to allow that call.
Oops. You're right. List.
Collect(fun item -> item) won't always return a list of lists. I'll edit my original post. – Narwe Jul 8 '10 at 20:18.
I Took the nested List idea and tried to write a neater version: type NestedListElement = //' | L of NestedListElement list //' | V of 'T //' let nested = LLV 1;V 2; V 3; V 4; LLV 5;V 6; V 7 let flatten n1 = let rec traverseNestedList nl = match nl with | V c -> c | L a -> List. Collect traverseNestedList a List. Collect traverseNestedList n1 let res7 = nested |> flatten.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.