Going through the lines, in turn, so you hopefully understand this better:
1| maximum' :: (Ord a) => [a] -> a 2| maximum' [] = error "maximum of empty list" 3| maximum' [x] = x 4| maximum' (x:xs) | x > maxTail = x | otherwise = maxTail where maxTail = maximum' xs
On line 1: you say you get a list a and return one element of this type. Plus, the items in this list should be consistent, so you can tidy them up.
On line 2: you have an edge where you say that if you get an empty list as input, this empty list cannot have the βhighest valueβ, so you end the function with an error.
On line 3: you have another edge where you say that if you get a list with 1 element, that element should be the highest element, so you return it.
On line 4: you use pattern matching to match the first element ( x ) and the rest of the list ( xs ). Then you check if x is greater than maxTail , where maxTail result of calling the maximum' function with the rest of the xs list.
If this x greater than maxTail , you return x , otherwise maxTail greater than x and you return maxTail .
I think an example with some numbers should also help here:
input:
[2, 5, 4, 13]
call:
maximum' [2, 5, 4, 13]
What's happening:
maximum' (x : xs) β β maximum' (2:[5, 4, 13]) β β result 13 x > maxTail = x β 2 > maximum' [5, 4, 13] = x //2 > 13 = 13 β βββββ β β β maximum' (x : xs) β β β β maximum' (5:[4, 13]) β β β β β x > maxTail = x β 5 > maximum' [4, 13] = x //5 > 13 = 13 β ββββββ β β β maximum' (x: xs) β β β β maximum' (4:[13]) β β β β β x > maxTail = x β 4 > maximum' [13] = x //4 > 13 = 13 β β β β maximum' [x] = x β maximum' [13] = 13 β ββββββββββββ
In your second example, this is about the same:
1| reverse' :: [a] -> [a] 2| reverse' [] = [] 3| reverse' (x:xs) = reverse' xs ++ [x]
On line 1: you say you get list a and return list a.
On line 2: you have an edge where you say that if you get an empty list, the inverted list is also empty.
On line 3: you again use pattern matching to match the first item ( x ) of the list and tail ( xs ).
Now you just use ++ to add two lists together, which is the first element of a list with a reverse tail.
And again, I hope the example is a little clearer:
input:
[1, 2, 3]
call:
reverse' [1, 2, 3]
What's happening:
reverse' (x : xs) β β reverse' (1:[2, 3]) β result [3, 2, 1] β β (reverse' [2, 3]) ++ [1] //[3, 2] ++ [1] = [3, 2, 1] β βββββ β β β reverse' (x: xs) β β β β reverse' (2:[3]) β β β β β (reverse' [3]) ++ [2] //[3] ++ [2] = [3, 2] β ββββ β β β β β reverse' (x:xs) β β β β reverse' (3:[]) β β β β β (reverse' []) ++ [3] //[] ++ [3] = [3] β β β β β β reverse' [] = [] β βββββββββββββββββββ