Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Haskell Turtle库中将“Shell Line”转换为“Text”?_Haskell_Haskell Turtle - Fatal编程技术网

如何在Haskell Turtle库中将“Shell Line”转换为“Text”?

如何在Haskell Turtle库中将“Shell Line”转换为“Text”?,haskell,haskell-turtle,Haskell,Haskell Turtle,考虑(inshell“echo A line of text.”为空)具有类型Shell行 问题:如何将这种类型的值转换为文本?您不能。见* 您只能从中获取IO内容。然而,您可能会得到一个IO文本,而且由于您正在使用shell,我怀疑您有一个IOmonad上下文 定义为* data FoldM m a b = -- FoldM step initial extract forall x . FoldM (x -> a -> m x) (m x) (x -> m b)

考虑
(inshell“echo A line of text.”为空)
具有类型
Shell行

问题:如何将这种类型的值转换为
文本?

您不能。见*

您只能从中获取
IO
内容。然而,您可能会得到一个
IO文本
,而且由于您正在使用shell,我怀疑您有一个
IO
monad上下文

定义为*

data FoldM m a b =
   -- FoldM step initial extract
   forall x . FoldM (x -> a -> m x) (m x) (x -> m b)
因此,我怀疑,如果我们能构造一个
折叠IO行文本
,那么我们就能得到我们需要的东西。这里有一个猜测(不是打字检查,这有点复杂,所以我可能会犯错误)

shellToText::Shell文本->IO文本
shellToText shell=foldIO shell foldm
哪里
foldm::foldm IO行文本
foldm=foldm(\accum-line->return(accum-lineToText-line))--Text->line->IO-Text
(返回Text.empty)——IO文本
return--Text->IO Text
通过使用,这可能会大大简化,但我将把它作为练习


(*)如果您不熟悉,第一个
forall
表示a,第二个表示a。

严格的救援功能:

strict::MonadIO io=>Shell行->io文本

所以,你可以

strict$inshell”回显一行文本。“空的”

并在IO文本monad中获取shell命令的输出

data FoldM m a b =
   -- FoldM step initial extract
   forall x . FoldM (x -> a -> m x) (m x) (x -> m b)
shellToText :: Shell Text -> IO Text
shellToText shell = foldIO shell foldm
    where
    foldm :: FoldM IO Line Text
    foldm = FoldM (\accum line -> return (accum <> lineToText line)) -- Text -> Line -> IO Text
                  (return Text.empty)                                -- IO Text
                  return                                             -- Text -> IO Text