-replaceNums [] = []
-replaceNums haystack
- | "one" `isPrefixOf` haystack = '1' : remainder
- | "two" `isPrefixOf` haystack = '2' : remainder
- | "three" `isPrefixOf` haystack = '3' : remainder
- | "four" `isPrefixOf` haystack = '4' : remainder
- | "five" `isPrefixOf` haystack = '5' : remainder
- | "six" `isPrefixOf` haystack = '6' : remainder
- | "seven" `isPrefixOf` haystack = '7' : remainder
- | "eight" `isPrefixOf` haystack = '8' : remainder
- | "nine" `isPrefixOf` haystack = '9' : remainder
- | otherwise = (head haystack) : remainder
- where remainder = replaceNums $ tail haystack
-
+replaceNums haystack = reverse $ foldl' go "" $ tails haystack
+ where go acc [] = acc
+ go acc xs
+ | "one" `isPrefixOf` xs = '1' : acc
+ | "two" `isPrefixOf` xs = '2' : acc
+ | "three" `isPrefixOf` xs = '3' : acc
+ | "four" `isPrefixOf` xs = '4' : acc
+ | "five" `isPrefixOf` xs = '5' : acc
+ | "six" `isPrefixOf` xs = '6' : acc
+ | "seven" `isPrefixOf` xs = '7' : acc
+ | "eight" `isPrefixOf` xs = '8' : acc
+ | "nine" `isPrefixOf` xs = '9' : acc
+ | isDigit (head xs) = (head xs) : acc
+ | otherwise = acc