我正在尝试遵循本教程:https://wiki.haskell.org/Tutorials/Programming_Haskell/String_IO。
在最后一部分 7扩展:使用SMP并行我复制代码但是无法使用此错误消息进行编译
/home/dhilst/parallelspell.hs:13:20: error:
Variable not in scope: chunk :: Int -> [String] -> t
我在Hoogle搜索了块并获得了 Data.Text.Internal.Lazy ,但这似乎是一个内部模块。无论如何我无法导入它。
以下是代码:
import Data.Set hiding (map)
import Data.Maybe
import Data.Char
import Text.Printf
import System.IO
import System.Environment
import Control.Concurrent
import Control.Monad
main = do
(f,g,n) <- readFiles
let dict = fromList (lines f)
work = chunk n (words g)
run n dict work
run n dict work = do
chan <- newChan
errs <- getChanContents chan
mapM_ (forkIO . thread chan dict) (zip [1..n] work)
wait n errs 0
wait n xs i = when (i < n) $ case xs of
Nothing : ys -> wait n ys $! i+1
Just s : ys -> putStrLn s >> wait n ys i
thread chan dict (me,xs) = do
mapM_ spellit xs
writeChan chan Nothing
where spellit w = when (spell dict w) $
writeChan chan . Just $ printf "Thread %d: %-25s" (me::Int) w
spell d w = w `notMember` d
readFiles = do
[s,n] <- getArgs
f <- readFile "/usr/share/dict/words"
g <- readFile s
return (f,g, read n)
这是编译行:
ghc -O --make -threaded parallelspell.hs
- 更新:我根据此任务编写自己的 chunk 版本:How to partition a list in Haskell?
chunk :: Int -> [a] -> [[a]]
chunk _ [] = []
chunk n xs = (take n xs) : (chunk n (drop n xs))
但是,这是否意味着我所关注的教程已经过时且过时了!?任何人都可以确认某一天是否已存在该功能,或者我是否遗漏了某些东西?
此致
答案 0 :(得分:3)
看起来教程忘了定义chunk
。我鼓励您更新Wiki以包含合适的定义。