在Haskell中,我想制作简单的concordancer。但它再次响应错误

时间:2013-10-28 13:24:27

标签: haskell

import System.IO
import Data.List
import Data.Char
printlist :: Show a => a -> IO ()
printlist x = putStr (show x)
main = do
     handle <- openFile "/usr/local/share/corpus" ReadMode
     text <- hGetContents handle
     let wlist = words text
         clist = map (\k -> take ((k + 15) - k + 1).drop (k - 10))(elemIndices "word" wlist)
printlist clist

我可以做些什么来完成我的工作 请给我一个答案或提示

1 个答案:

答案 0 :(得分:3)

我感觉很好,所以我在这里解决了错误

import Data.List

printlist :: Show a => a -> IO ()
printlist = putStr . show

main = do
     text <- readFile "/usr/local/share/corpus" -- removed useless handle
     let clist = zipWith (flip ($)) (repeat text)
                -- ^ applied each function to file
                -- since you currently had
                -- clist :: [String -> String]
                 . map (\k -> take 16 . drop (k-10))
                 . elemIndices "word"
                 $ words text -- inlined wlist
     printlist clist -- fixed indenting

现在,它的作用是生成String -> String类型的函数列表,并将它们中的每一个应用于文件/usr/local/share/corpus并打印结果。

我想地图部分可以重写为

(.:) = (.) . (.)
infixr 9 .:

map (take 16 .: drop . subtract 10)

这可以说是更漂亮。