undefined no better than using error . In fact, undefined in foreplay is defined as
undefined = error "Prelude.undefined"
Now a function that cannot lead to error is called a "full function", i.e. Valid for all input values.
The split function that you currently implemented has a signature
split :: [a] -> ([a], a)
This is a problem because the signature of type promises is that the result always contains a list and an element, which is obviously impossible to provide for empty lists of a generic type.
The canonical way in Haskell to solve this problem is to change the type signature, which means that sometimes we don’t have a valid value for the second element.
split :: [a] -> ([a], Maybe a)
Now you can write the correct implementation for the case when you get an empty list
split [] = ([], Nothing) split xs = split' [] xs where split' acc (x:[]) = (reverse acc, Just x) split' acc (x:xs) = split' (x:acc) xs
Now you can find the missing value by the pattern
let (init', last') = split xs in case last' of Nothing -> ... -- do something if we don't have a value Just x -> ... -- do something with value x
shang
source share