Which of these are valid Haskell and which are not? Rewrite in cons notation.
Which of these are valid Haskell, and which are not? Rewrite in comma and bracket notation.
Can Haskell have lists of lists of lists? Why or why not?
Why is the following list invalid in Haskell? Don't worry too much if you don't get this one yet.
1 and 2 aren't valid Haskell, 3 is valid:
1:2:3::. 1, 2 and 3 are integers, while  is a list.
1:(2:3:):4:. Again, 1 and 4 are integers, and 2:3: is a list of integers.
(1:2:3:)::. This is valid Haskell, as 1:2:3: is a list of integers, and  is an empty list (of any type).
The first four are valid Haskell. The fifth is not.
[,[1,2,3],[4,5,6]]. Both [1,2,3] and [4,5,6] are lists of integers. The whole list is a list of lists of integers. We can cons an empty list (of any type) in front of it.
[]. Not the empty list!. This is a list containing an empty list. The list itself is not empty, because it has one element!
[,]. This is a list containing two empty lists.
[,]. This is the same list as the previous, but with a  as the first element instead of . Since the list was a lists of lists, it now has become a list of integers.
[["hi"], ]. ["hi"] is a list of Chars while  is a list of integers.
Yes, this is possible. For example: [[,,],[,,],[,,]]. Why? You can make a list of anything! If you already have a list of some type, then you can make a list of lists of that type. The example list would be written as: ((1:):(2:):(3:):):((4:):(5:):(6:):):((7:):(8:):(9:):):
The list [[1,2],3,[4,5]] is not valid because it is equivalent to (1:2:):3:(4:5:):, where we try to cons elements that have different types (i.e. list and numeric). Lists in Haskell must be type-homogeneous. A valid list would be [[1,2],,[4,5]], equivalent to (1:2:):(3:):(4:5:):.
Use a combination of fst and snd to extract the 4 from the tuple (("Hello", 4), True).
Normal chess notation is somewhat different to ours: it numbers the rows from 1-8 and the columns a-h; and the column label is customarily given first. Could we label a specific point with a character and a number, like ('a', 4)? What important difference with lists does this illustrate?
Write a function which returns the head and the tail of a list as the first and second elements of a tuple.
Use head and tail to write a function which gives the fifth element of a list. Then, make a critique of it, pointing out any annoyances and pitfalls you can identify.
snd (fst (("Hello", 4), True)) returns 4.
Yes, we can! The difference between a tuple and a list is that all elements of a list must be of the same type (integers, booleans, etc.), but you can add elements to the list. The elements of a tuple can be of any type you want, like a tuple of an integer and a character in (4, 'a'), but you can't change its size.
headAndTail list =(head list,tail list)
fifthElement list =head(tail(tail(tail(tail list))))
Inconveniences of this implementation include the clunkiness of the definition, its lack of generality (what if we wanted to get the fourth or the sixth or the nineteenth element?), and the fact it will cause the program to crash whenever it is passed a list with less than five elements.