我試圖從F#中的99個Haskell問題中解決任務。 任務#7看起來非常簡單,解決方案可以在很多地方找到。除了我試過並通過Google搜索發現的前幾個解決方案(例如https://github.com/paks/99-FSharp-Problems/blob/master/P01to10/Solutions.fs)是錯誤的這一事實。99 Haskell問題。 F#中的#7 - stackoverflowexception
我的例子很簡單。 我試圖建立極深嵌套結構,並把它摺疊
type NestedList<'a> =
| Elem of 'a
| NestedList of NestedList<'a> list
let flatten list =
//
(* StackOverflowException
| Elem(a) as i -> [a]
| NestedList(nest) -> nest |> Seq.map myFlatten |> List.concat
*)
// Both are failed with stackoverflowexception too https://github.com/paks/99-FSharp-Problems/blob/master/P01to10/Solutions.fs
let insideGen count =
let rec insideGen' count agg =
match count with
| 0 -> agg
| _ ->
insideGen' (count-1) (NestedList([Elem(count); agg]))
insideGen' count (Elem(-1))
let z = insideGen 50000
let res = flatten z
我試圖重寫CPS風格的解決方案,但eiter我做錯事或考慮不正確的方向 - 一切,我」已經嘗試不起作用。
任何建議?
p.s. Haskell解決方案,至少在嵌套級別爲50000的嵌套結構上運行緩慢,但沒有堆棧溢出。
你用什麼選項來編譯? –
我試圖在VS 2013發佈模式下運行(Ctrl + F5),並打開「生成尾呼叫」複選框 – galkk