【问题标题】:How to import ‘System.IO.Strict’? (HASKELL)如何导入“System.IO.Strict”? (哈斯克尔)
【发布时间】:2021-11-16 16:13:17
【问题描述】:

我正在尝试打开一个文件并将第一行移到最后,如下所示:

-- Moves the first line in a file to the end of the file (first line becomes last line)
flyttLn :: FilePath -> IO ()
flyttLn fn = do
    fh <- openFile fn ReadMode
    content <- hGetContents fh
    --putStrLn content 
    let
        (l1:rest) = lines content
        newContent = unlines (rest++[l1])
    hClose fh
    fh2 <- openFile fn WriteMode
    hPutStr fh2 newContent
    hClose fh2

由于惰性评估,这给了我一个错误。所以基于this的问题,我首先尝试打印文件的内容,然后重新编写它们。哪个有效,除了我不想在终端中打印整个文件。所以我尝试像这样导入System.IO.Strict

import qualified System.IO.Strict as SIO

但是 VS Code 给了我一个错误提示

Could not find module ‘System.IO.Strict’
It is not a module in the current program, or in any known package.not found

我曾尝试寻找类似的问题,但我只找到this的问题,并没有帮助我解决我的问题。

如何正确打开文件并编辑其内容,而无需在终端中打印整个文件?如何正确导入 System.IO.Strict?

【问题讨论】:

    标签: haskell visual-studio-code


    【解决方案1】:

    模块System.IO.Strict 定义在strict 包中(我找到了这个using hoogle)。

    我建议自己制作一个 cabal 或 stack 包,并将 strict 添加到依赖项中。 对于阴谋集团,我会推荐the official getting started guide here。对于堆栈,您可以查看the official guide here

    【讨论】:

      【解决方案2】:

      我不必用这个导入System.IO.Strict

      flyttLn :: FilePath -> IO ()
      flyttLn file = 
          do 
              fh <- openFile file ReadMode
              content <- hGetContents fh
              let 
                  (l1:rest) = lines content
                  newContent = unlines (rest++[l1])
              (fn2, fh2) <- openTempFile "." "temp"
              hPutStr fh2 newContent
              hClose fh
              hClose fh2
              removeFile file
              renameFile fn2 file
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多