-4
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

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

4

1 回答 1

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)

可以说哪个更漂亮。

于 2013-10-28T15:00:01.337 回答