Question: Consider the following function for finding the second-to-last element of a list:

```
myButLast (x:xs) = if length xs > 1 then myButLast xs else x

```

This is an O(n^2) algorithm, because `length xs` is O(n) and is called O(n) times. What is the most elegant way to write this in Haskell such that `length` stops once it gets past 1, so that the algorithm is O(n)?

Answer: 
Another solution:

```
myButLast [] = error "List is empty"
myButLast [x] = error "List is a singleton"
myButLast xs = last $ init xs

```
Here is my solution using the tail function from prelude

```
myButLast :: [Int] -> Maybe Int
myButLast (x:xs) | ((null xs) || null (tail xs)) = Just x
                 | otherwise                     = myButLast(xs)
myButLast []                                     = Nothing

```