Prelude Implementation of Last overlapping patterns?

The standard Prelude implementation of 'last' is as follows \begin{code} last :: [a] -> a last [x] = x last (_:xs) = last xs last [] = error "Prelude.last: empty list" \end{code} Isn't [x] equivalent to (x:[]) hence wouldn't [1] match both [x] and (_:xs)? If that's the case then we would have last [1] == 1 and last [1] == last [] but that doesn't happen. Why?

Because patterns are matched in order and the first one match is taken.
On 24 Dec 2011 17:49, "Mark Stoehr"
The standard Prelude implementation of 'last' is as follows \begin{code} last :: [a] -> a last [x] = x last (_:xs) = last xs last [] = error "Prelude.last: empty list" \end{code}
Isn't [x] equivalent to (x:[]) hence wouldn't [1] match both [x] and (_:xs)? If that's the case then we would have last [1] == 1 and last [1] == last []
but that doesn't happen. Why?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Dec 24, 2011 12:53 PM, "Benjamin Edwards"
Because patterns are matched in order and the first one match is taken.
This is very expressive. For example, you can write f 0 = 0 f x = 1 / x
On 24 Dec 2011 17:49, "Mark Stoehr"
wrote: The standard Prelude implementation of 'last' is as follows \begin{code} last :: [a] -> a last [x] = x last (_:xs) = last xs last [] = error "Prelude.last: empty list" \end{code}
Isn't [x] equivalent to (x:[]) hence wouldn't [1] match both [x] and (_:xs)? If that's the case then we would have last [1] == 1 and last [1] == last []
but that doesn't happen. Why?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Benjamin Edwards
-
Mark Stoehr
-
Tom Murphy