Fth*_*der 3 haskell string-interpolation quasiquotes
我怎样才能像这样插值:
{-# LANGUAGE QuasiQuotes #-}
import Text.RawString.QQ
myText :: Text -> Text
myText myVariable = [r|line one
line two
line tree
${ myVariable }
line five|]
myText' :: Text
myText' = myText "line four"
Run Code Online (Sandbox Code Playgroud)
${ myVariable }作为文字打印,而不是插值,在这种情况下我可以做类似的事情来插值吗?
准引用器r不实现插值。它仅适用于原始字符串。你需要另一个准报价者。
完整代码:
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
import Data.Text (Text)
import Text.RawString.QQ (r)
import NeatInterpolation (text)
rQuote :: Text -> Text
rQuote myVariable = [r|line one
line two
line tree
${ myVariable }
line five|]
neatQuote :: Text -> Text
neatQuote myVariable = [text|line one
line two
line tree
$myVariable
line five|]
rText, neatText :: Text
rText = rQuote "line four"
neatText = neatQuote "line four"
Run Code Online (Sandbox Code Playgroud)
在ghci:
*Main> import Data.Text.IO as TIO
*Main TIO> TIO.putStrLn rText
line one
line two
line tree
${ myVariable }
line five
*Main TIO> TIO.putStrLn neatText
line one
line two
line tree
line four
line five
Run Code Online (Sandbox Code Playgroud)