我試圖製作一個包含子列表的列表,如[1, 2, [3, 4], [3, [4, 5]]]
。在Haskell中創建以下列表[1,2,[3,4],[3,[4,5]]]
看來我應該定義一個新類型。我試過了:
data NestedList a = Int a | List [NestedList a]
但是我覺得它錯了,或者說我不知道如何使用它。我對Haskell很新,我不確定這個表達的含義。
這是否意味着這是有「型」 Int
與參數a
和帶有參數[NestedList a]
「類型」 List
?上述
的data
表達從solution帶到99 Haskell questions
(**)拼合嵌套列表結構的第七鍛鍊。
data NestedList a = Elem a | List [NestedList a] flatten :: NestedList a -> [a] flatten (Elem x) = [x] flatten (List x) = concatMap flatten x
,但是當我在ghci
打電話flatten [[1,2], 3]
我得到的
couldn't match expected type 'NestedList a0' with actual type '[t0]'
http://codegolf.stackexchange.com/questions/113238/is-it-true-ask-jelly/113416#113416 – leftaroundabout 2017-03-21 18:07:04