I'm using advent of code as an excuse to learn haskell, and made this pattern-matching map, which feels slightly abominable.
extractNumeral :: String -> (Maybe Char, String) extractNumeral ('o':'n':'e':xs) = (Just '1', 'e':xs) extractNumeral ('t':'w':'o':xs) = (Just '2', 'o':xs) extractNumeral ('t':'h':'r':'e':'e':xs) = (Just '3', 'e':xs) extractNumeral ('f':'o':'u':'r':xs) = (Just '4', xs) extractNumeral ('f':'i':'v':'e':xs) = (Just '5', 'e':xs) extractNumeral ('s':'i':'x':xs) = (Just '6', xs) extractNumeral ('s':'e':'v':'e':'n':xs) = (Just '7', 'n':xs) extractNumeral ('e':'i':'g':'h':'t':xs) = (Just '8', 't':xs) extractNumeral ('n':'i':'n':'e':xs) = (Just '9', 'e':xs) extractNumeral (x:xs) = if isNumeral x then (Just x, xs) else (Nothing, xs) extractNumeral [] = (Nothing, [])
I don't know if i'm proud or disappointed by this. This feels like it could be done in a smoother fashion, but then again one gotta do some kind of lookup of spelling for letters anyway.
How should I have done this? Also, is there some way to avoid the 's':'p':'e':'l':'l':'i':'n':'g':[] of letters like this?

















