2010-08-30 53 views
2

我正在學習haskell。目前通過99 questions工作,停留在第7一點點:haskell:99個問題#7:異構列表

問題7 (**)拼合嵌套表結構。

轉換一個列表,可能通過用元素(遞歸)替換每個列表來將列表作爲元素保存到「扁平」列表中。

例哈斯克爾:

*Main> flatten (Elem 5) 
[5] 
*Main> flatten (List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]]) 
[1,2,3,4,5] 
*Main> flatten (List []) 
[] 

何處來自ElemList?我需要做些什麼才能在我的程序中使用它們? (或者這個問題是否假定我必須爲這些類型定義一個新類型 - 如果這是答案,我會離開並重新閱讀本教程的這一部分...)

回答

8

這些只是某些類型的構造函數,例如

data ListType a = Elem a | List [ListType a] 
+0

請注意,ListType是一個非常糟糕的名字。它實際上是一棵樹。 – Martijn 2010-09-03 12:41:08