I would like to paste in a large number constant into my Haskell code, and for readability I would like to have it formatted over several lines instead of one line.
Is this possible?
I would like to paste in a large number constant into my Haskell code, and for readability I would like to have it formatted over several lines instead of one line.
Is this possible?
This is a possible approach. I'm not completely sure about it, though. There might be an easier way.
largeConstant :: Integer
largeConstant = read $
"12345" ++
"12345" ++
"12345"
Alternatively, we could use multiline string literals, even though they are not very commonly used in Haskell.
largeConstant :: Integer
largeConstant = read
"12345\
\12345\
\12345"
Enabling CPP is also an option, but seems a bit overkill.
largeConstant = 12345\
12345\
12345
You could make a quasiquoter. Might even want to upload it to hackage:
module X where
import Language.Haskell.TH
import Language.Haskell.TH.Quote
import Data.Char
iQQ :: QuasiQuoter
iQQ = QuasiQuoter {
quoteExp = return . LitE . IntegerL . read . filter isDigit,
quotePat = \_ -> fail "illegal integer QuasiQuote \
\(allowed as expression only, used as a pattern)",
quoteType = \_ -> fail "illegal integer QuasiQuote \
\(allowed as expression only, used as a type)",
quoteDec = \_ -> fail "illegal integer QuasiQuote \
\(allowed as expression only, used as a declaration)"
}
And the use:
{-# LANGUAGE QuasiQuotes #-}
import X
value = [iQQ|123
456|]
Resulting in:
Ok, two modules loaded.
*Main> value
123456
Or a larger number:
value = [iQQ|
44444444444444444444444444444444444444444444444444444444444444444444444444444444
45555555555555555555555555555555555555555555555555555555555555555555555555555555
66666666666666666666666666666666666666666666666666666666666666666666666666666666
|]
And in GHCi:
Ok, two modules loaded.
*Main> value
444444444444444444444444444444444444444444444444444444444444444444444444444444444555555555555555555555555555555555555555555555555555555555555555555555555555555566666666666666666666666666666666666666666666666666666666666666666666666666666666