开发者

haskell: 99 questions #7: heterogeneous list

I'm learning haskell. Currently working through 99 questions, a little bit stuck on #7:

Problem 7 (**) Flatten a nested list structure.

Transform a list, possibly holding l开发者_如何转开发ists as elements into a `flat' list by replacing each list with its elements (recursively).

Example in Haskell:

*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 [])
[]

Where do Elem and List come from? What do I have to do to be able to use them in my program? (Or does the question assume that I have to define a new type for these -- if that's the answer I'll go off and reread that section of the tutorial...)


Those are just constructor of some types, e.g.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜