I'm looking for general review and improvements on this code that safely gets the next element of a list (and wraps to the first element if you exceed the list). It just seems a little excessive for how simple of a task it is.It should also be noted that this function will only work for lists that do not contain duplicate elements. Unless I modify it to take index numbers instead, I can't see that restriction changing. If length list < 2, it will return Nothing. nextElem :: Eq e => e -> [e] -> Maybe enextElem e xs = nextElem' e (shiftList e xs)    where        shiftList e xs = let (ya, yb) = break (== e) xs in yb ++ ya        nextElem' _ []      = Nothing        nextElem' _ (x : [])= Nothing        nextElem' e (x : xs)            | x == e    = Just (head xs)            | otherwise = nextElem' e xs
Safely getting the next element of a list
haskell;circular list
Using functions out of base you can implement this as a fairly readable one-liner.import Data.Maybe (listToMaybe)nextElem :: Eq a => a -> [a] -> Maybe anextElem e xs = listToMaybe . drop 1 . dropWhile (/= e) $ xs ++ (take 1 xs)To break that down, since the list is supposed to be circular we safely pop the first element off the front of xs with take 1 (we either get the first element, or an empty list) and append that to the back of xs to get the wrapping property. (As an aside, this is complicated a small amount by being efficient, an equally correct solution to having a circular list would be xs ++ xs, but that would end up checking the entire list for equality twice if e isn't present.)Next we drop all elements from the front of the list that are not equal to e, which either produces the empty list if the element isn't found, or the first tail of the list fronted by e, i.e., (e:...), so we use drop 1 as a safe version of tail (to again handle the case of an empty list) and end up with a list where the head element is our answer. Then listToMaybe produces Just the first element of the list, or Nothing if the list is empty.In principle this isn't much different from your original solution, I've just shrink-wrapped it down to the constituent pieces and arranged control through function composition.