Haskell macrosGautier DI FOLCO July 19, 2023 [dev] #haskell
A while ago I was looking for something in
base, when I landed in
Which made me realized that Haskell has pure C/C++ macros, not only
#else to check Cabal-defined flags.
A dirty thought crossed my mind, we could define something like:
type Rule = Int -> Maybe String RULE(name, n, text) \ (name :: Rule) = \x -> if mod x n == 0 then Just "text" else Nothing RULE (fizz, 3, "Fizz") RULE (buzz, 5, "Buzz") RULE (fizzBuzz, 15, "FizzBuzz")
Once evaluated, it gives:
-- ... type Rule = Int -> Maybe String (fizz :: Rule) = \x -> if mod x 3 == 0 then Just " Fizz" else Nothing (buzz :: Rule) = \x -> if mod x 5 == 0 then Just " Buzz" else Nothing (fizzBuzz :: Rule) = \x -> if mod x 15 == 0 then Just " FizzBuzz" else Nothing
A bit dirty but we have what we want:
type Rule :: Type type Rule = Int -> Maybe String
To go further : the excellent Aelve Guide.