首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Haskell QuasiQuotes Text.RawString.QQ插值

Haskell QuasiQuotes Text.RawString.QQ插值
EN

Stack Overflow用户
提问于 2018-06-05 04:21:15
回答 2查看 418关注 0票数 1

我如何像这样进行插值:

代码语言:javascript
复制
{-# 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"

${ myVariable }打印为文字,而不是插值,在这种情况下,我可以做一些类似于插值的事情吗?

EN

回答 2

Stack Overflow用户

发布于 2018-06-06 11:08:16

Quasi r不实现插值。它只适用于原始字符串。您需要另一个准引号。

完整代码:

代码语言:javascript
复制
{-# 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"

ghci

代码语言:javascript
复制
*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
票数 3
EN

Stack Overflow用户

发布于 2018-06-05 19:16:51

我实现目标的唯一方法就是连接:

代码语言:javascript
复制
{-# LANGUAGE QuasiQuotes #-}
import Text.RawString.QQ

myText :: Text -> Text
myText myVariable = [r|line one
line two
line tree
|] <> myVariable <> [r|
line five|]

myText' :: Text
myText' = myText "line four"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50688200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档