Is it possible to define a function using Template Haskell? For example
convertStringToValue :: String -> Int
convertStringToValue \"three\" = 3
convertStrin
Yes
import Language.Haskell.TH
generateDict :: String -> [(String, Int)] -> Q [Dec]
generateDict fname sns = do
let clauses = map clause sns
return $ [FunD (mkName fname) clauses]
where clause (s,n) =
Clause [LitP . IntegerL $ toInteger n]
(NormalB . LitE $ StringL s )
[]
And then
generateDict "myDict" $ zip (words "One Two Tree Four") [1..]
myDict 1 -- => "One"