12

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

?

3 Answers 3

22

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)
Sign up to request clarification or add additional context in comments.

Comments

19

You can use the same #define trick in Haskell if you turn on the CPP language extension. Or you can use Template Haskell to get the same effect.

But in pure Haskell it is impossible, because it violates the principle of alpha conversion, i.e., renaming bound variables (in a hygienic way) should not change the program semantics.

3 Comments

Couldn't achieve this behaviour with {-# LANGUAGE CPP #-}. Seems GHC C preprocessor just ignores #.
Matvey, I think it is just that Haskell's printf doesn't take a tuple, but, in the first instance, just the format specification. Then it takes further arguments successively according to its interpretation of the format in question. "%s = %d\n" leaves room for two arguments. So you'd write e.g. printf "%s = %d\n" "five" 5 rather than printf( "%s = %d\n", "five" , 5). I paste something that works below; see what you can make of it.
By the way, a simple illustration of an extensive use of CPP to avoid boilerplate is here hackage.haskell.org/packages/archive/uu-parsinglib/2.7.3/doc/… Note the uncurrying in the message to the preprocessor: #define DEMO(p,i) demo "p" i p
4
{-#LANGUAGE CPP#-}
import Text.Printf
#define print( a ) printf "%s = %d\n" (show a) a 

main = do  let a = (5 :: Int)
           print( a ) 



 $ ghci a.hs
 *Main> main
 5 = 5

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.