Map from Char to String in Haskell
I am trying to figure out how to improve the transliteration from German umlauts to ASCII for identifiers id
in Pandoc. Currently there is only a mapping Char -> Maybe Char
that converts ä
to a
and ß
to Nothing
, etc., but the most common convention is ä
to map to ae
and ß
to ss
, etc. Here's what I have so far:
import Data.Char (isAscii)
import qualified Data.Map as M
asciiMap' :: M.Map Char String
asciiMap' = M.fromList
[('\196',"Ae")
,('\214',"Oe")
,('\220',"Ue")
,('\223',"ss")
,('\228',"ae")
,('\246',"oe")
,('\252',"ue")
]
toAsciiStr :: Char -> String
toAsciiStr c | isAscii c = [c]
| otherwise = M.findWithDefault "" c asciiMap'
myTranslit :: String -> String
myTranslit [] = []
myTranslit (x:xs) = toAsciiStr x ++ myTranslit xs
My question is about myTranslit
.
Perhaps there is already a built-in map display function someMap :: (a -> [a]) -> [a] -> [a]
?
source to share
Yes, you are looking for one that concatenates the output after matching. Since a , it can specialize in and further (with and ) on . Note that is an alias for , so a is nothing more than a list of -acters. concatMap :: Foldable t => (a -> [b]) -> t a -> [b]
[]
Foldable
concatMap :: (a -> [b]) -> [a] -> [b]
a ~ Char
b ~ Char
concatMap :: (Char -> [Char]) -> [Char] -> [Char]
String
type String = [Char]
String
Char
Thus, you can use:
myTranslit :: String -> String
myTranslit = concatMap toAsciiStr
source to share