如何打印记录类型?

时间:2017-07-23 12:20:41

标签: haskell

鉴于像Example {a = "a", b = "b", c = "c"}这样的记录,我怎样才能打印任何这样的记录类型,以便于阅读?

像:

Example {
  a = "a"
, b = "b"
, c = "c"
}

会很理想。

我已尝试pretty-show包中的pPrint,但这似乎与show输出不同。

2 个答案:

答案 0 :(得分:5)

正如@SwiftsNamesake指出的那样尝试使用Text.Show.Pretty

import Text.Show.Pretty 

data Example = Example
    { a :: String
    , b :: String
    , c :: String
    } deriving (Show) 

main = print $ ppShow Example {a = "a", b = "b", c = "c"}

答案 1 :(得分:5)

这可以通过以下方式实现:pretty-simple

ghci> import Text.Pretty.Simple (pPrint)
ghci> data Example = Example { a, b, c :: String } deriving Show
ghci> pPrint Example {a = "a", b = "b", c = "c"}
Example 
    { a = "a"
    , b = "b"
    , c = "c"
    }
相关问题