I came to haskell having some c background knowledge and wondering is there an analog to
#define print( a ) printf( "%s = %d\n", #a, a )
int a = 5;
print( a );
that should print
a = 5
?
Here's the TH solution augustss mentioned:
{-# LANGUAGE TemplateHaskell #-}
module Pr where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
pr :: Name -> ExpQ
pr n = [| putStrLn ( $(lift (nameBase n ++ " = ")) ++ show $(varE n) ) |]
then in another module:
{-# LANGUAGE TemplateHaskell #-}
import Pr
a = "hello"
main = $(pr 'a)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With