Nov 18, 2016
Lists of lists can be tricky sometimes because a list of things does not have the same type as a thing all by itself. The type Int is different from [Int]. Let's sort through these implications with a few exercises: "Exercises Which of these are valid Haskell and which are not? Rewrite in cons notation.[1,2,3,].[1,[2,3],4].[[1,2,3],].Which of these are valid Haskell, and which are not? Rewrite in comma and bracket notation.:[[1,2,3],[4,5,6]].:.::.::.["hi"]::.Can Haskell have lists of lists of lists? Why or why not?.Why is the following list invalid in Haskell?[[1,2],3,[4,5]]." Lists of different types of things cannot be consed, but the empty list can be consed with lists of anything. For example, :[[1, 2], [1, 2, 3]] is valid and will produce [, [1, 2], [1, 2, 3]], and :[[1, 2], [1, 2, 3]] is valid and will produce [, [1, 2], [1, 2, 3]], but ['a']:[[1, 2], [1, 2, 3]] will produce an error message. Lists of lists allow us to express some kinds of complicated, structured data (two-dimensional matrices, for example). They are also one of the places where the Haskell type system truly shines. Human programmers (including this wikibook co-author) get confused all the time when working with lists of lists, and having restrictions on types often helps in wading through the potential mess.