首页 > 解决方案 > Haskell Stack 显示错误“找不到包 unix 的包 ID”。如何修复此错误?

问题描述

我正在尝试运行使用unix包 using的 Haskell 脚本,stack但它显示错误Could not find package id of package unix

命令提示符:

PS D:\Programming\Haskell> stack helpful_math.hs
Could not find package id of package unix
PS D:\Programming\Haskell> stack --version
Version 2.3.3, Git revision cb44d51bed48b723a5deb08c3348c0b3ccfc437e x86_64 hpack-0.33.0
PS D:\Programming\Haskell> stack ghc -- --version
The Glorious Glasgow Haskell Compilation System, version 8.10.4

Haskell 脚本help_math.hs是:

#!stack
{- stack runghc
    --package split --package extra --package unix
-}
import Data.Char (toLower)
import Data.List (sort, intercalate)
import Data.List.Split (splitOn)
import Data.List.Extra (trim)
import Control.Monad (forever)
import Control.Monad.Extra (whileM)
import Control.Concurrent (myThreadId, throwTo)
import Control.Exception (UserInterrupt)
import System.IO (hFlush, stdout)
import System.Posix.Signals (Catch, installHandler, keyboardSignal)

promptString = "\n> "

prompt :: String -> IO String
prompt msg = do
  putStr (msg ++ promptString)
  hFlush stdout
  trim <$> getLine

exitOnCtrlC :: IO String
exitOnCtrlC = do
  tid <- myThreadId
  installHandler keyboardSignal (Catch (throwTo tid UserInterrupt)) Nothing

solve :: String -> String
solve = intercalate " + " . map show . sort . map readNum . splitOn "+"
  where readNum :: String -> Int
        readNum = read . trim

main = do
  putStrLn "Welcome to Helpful Math Program!"
  --putStrLn "Press Ctrl-C to exit at any point"
  whileM $ do
    input <- prompt "Enter sum expression"
    putStr "Answer is: "
    putStrLn . solve $ input
    line <- map toLower <$> prompt "Do you want to quit? (y | n)"
    return $ line != "y"

标签: haskellhaskell-stack

解决方案


我自己找到了答案。这是一个愚蠢的错误——我忘记了 Windows 不支持 unix。所以代码无论如何都不会在 Windows 上运行。


推荐阅读