What does [a] stand for exactly?
haskell
Solution
As a type, `[a]` does stand for "a list of any size of `a`s". As a pattern however, `[a]` stands for "a list containing exactly one element, which shall henceforth be known by the name `a`". Similarly `[a,b]` would mean "a list containing two elements, the first of which shall be known as `a` and the second of which shall be known as `b`" and so. `[]`, as you already seem to know, stands for "a list containing exactly 0 elements".
This is analogous to how you'd write list literals as expressions. I.e. if you write `myList = []`, `myList` is the empty list and if you write `myList = [x]`, `myList` is a list containing exactly one element, which is the value of the variable `x`.
Problem
I'm doing some exercises from "Real World Haskell". One is to design a safe version of `init :: [a] -> [a]`. I'm supposed to start from `safeInit :: [a] -> Maybe [a]` This is what I have at the moment. ``` safeInit :: [a] -> Maybe [a] safeInit [] = Nothing safeInit [a] = if length [a] <= 1 then Nothing else Just (take (length [a] -1) [a]) ``` In GCHi, when testing `safeInit [1,2]` I get the error message * Exception: ch4exercise.hs:(21,1)-(24,44): Non-exhaustive patterns in function safeInit I was under the impression that `[a]` simply stands for a list (of any size) of `a`'s. What am I doing wrong?