如何在Haskell中创建更易读的多行​​字符串?

时间:2014-11-02 22:29:37

标签: haskell

我是Haskell的新手,正在测试JSON序列化。以下是测试用例的样子:

{-# LANGUAGE OverloadedStrings #-}

module WetlandsTest where

import Control.Exception (evaluate)
import Test.Hspec
import Wetlands

main :: IO ()
main = hspec $ do
  describe "wetlands" $ do
    describe "spotting" $ do
      it "returns a json encoded spotting" $ do
        let record = spotting "Snowy Egret" "California" "low tide"
        record `shouldBe` "{\"bird\":\"Snowy Eget\",\"state\":\"California\",\"meta\":\"low tide\"}"

有没有办法以更易读的方式编写它?也许有些东西:

record `shouldBe` """
{"bird":"Snowy Eget","city":"California","meta":"low tide"}
"""

这不一定是多行字符串,但是如果你对JSON进行了美化,那么它就是。只是想知道。

1 个答案:

答案 0 :(得分:10)

只需使用准引号扩展名和string-qq包:

{-# LANGUAGE QuasiQuotes #-}
import Data.String.QQ

someString :: String
someString = [s|
This is"
some string with "" quotes and stuff"!
|]

输出:

*Main> someString 
"This is\"\nsome string with \"\" quotes and stuff\"!\n"