Building lists
editExercises 

let myCons list thing = 
 This won't work. [True, False] is a list of booleans, 3 is an integer.
let cons8 list = 8:list
,let cons8 = (:) 8
andlet cons8 list = (:) 8 list
are all valid functions.cons8 []
returns[8]
cons8 [1,2,3]
returns[8,1,2,3]
cons8 [True,False]
gives a type error. This is the same mistake as in exercise 1.let foo = cons8 [1,2,3]
gives no output message, but foo is[8,1,2,3]
. Try it!cons8 foo
(assuming you did 2.4) returns[8,8,1,2,3]
 As recognized
let cons8 list = list:8
does not work, cause 8 is not a list, butlet cons8 list = list ++ [8]
will work since (++) concatenates 2 lists let myCons list thing = thing : list
,let myCons list thing = (:) thing list
are both valid functions.
Lists within lists
editExercises 


 1 and 2 aren't valid Haskell, 3 is valid:
1:2:3:[]:[]
.1
,2
and3
are integers, while[]
is a list.1:(2:3:[]):4:[]
. Again,1
and4
are integers, and2:3:[]
is a list of integers.(1:2:3:[]):[]:[]
. This is valid Haskell, as1: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.[[1],[]]
. This is the same list as the previous, but with a[1]
as the first element instead of[]
. Since the list was a lists of lists, it now has become a list of list of integers.[["hi"], [1]]
. ["hi"] is a list of Chars while [1] is a list of integers.
 Yes, this is possible. For example:
[[[1],[2],[3]],[[4],[5],[6]],[[7],[8],[9]]]
. 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 typehomogeneous. A valid list would be[[1,2],[3],[4,5]]
, equivalent to(1:2:[]):(3:[]):(4:5:[]):[]
.
A different notion of many
editExercises 


(4,"hello",True)
 They all are! Tuples aren't restricted to types.
 A tuple of two integers.
 A tuple of an integer and a string.
 A tuple of a boolean and two strings.
 The empty tuple
()
is pronounced "unit"

 Maybe the creators of Haskell wanted to limit the functionality of tuples to discourage their overuse. The practice of feeding functions consable tuples, instead of lists and tuples, may have made functions more complex, therefore harder to read, write and maintain.
 Unlike lists, we would obtain a tuple with a different type, because the tuple size would be bigger.
Tuples within lists and other combinations
editExercises 



 Not valid.
(2,3)
is a tuple, and cons,(:)
, only works with lists.  Not valid.
(2,3)
is a tuple, and cons,(:)
, only works with lists.  Valid. You'll get a list of tuples of an integer and an integer:
[(2,4)]
 Not valid. All elements of a list must be of the same type.
(2,4)
and(5,5)
are tuples of an integer and an integer, but('a','b')
is a tuple of a character and a character.  Valid. This is a tuple of a list of integers and a list of integers.
 Not valid.
Retrieving values
editExercises 


snd (fst (("Hello", 4), True))
returns4
. 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)
 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.
fifthElement list = head (tail (tail (tail (tail list))))
Polymorphic types
editExercises 

Give type signatures for the following functions:

headAndTail :: [a] > (a, [a])
fifthElement :: [a] > a
h x y z :: Int > a > b > Char  y and z are not actually used, so their types could be anything