【发布时间】:2017-02-11 12:01:28
【问题描述】:
combinationIO :: Int -> [a] -> IO [[a]]
combinationIO 0 _ = return [[]]
combinationIO _ [] = return []
combinationIO n (x:xs) = do res <- (map (x:) (combinationIO (n-1) xs)) ++ (combinationIO n xs)
putStrLn $ (show n) ++ show " : (" ++ show (x,xs) ++ show ") = " ++ show res
return res
我在某个站点上看到了这个示例(如下),我想知道它是如何工作的,所以我在其中添加了一些 IO 操作。但是,ghci 给了我一个类型错误。有什么问题?
combination2 :: Int -> [a] -> [[a]]
combination2 0 _ = [[]]
combination2 _ [] = []
combination2 n (x:xs) = (map (x:) (combination2 (n-1) xs)) ++ (combination2 n xs)
【问题讨论】:
-
错误是什么?
-
如果你的代码确实和你放在这里的一样,那么你有一个缩进问题:
putStrLn和return必须在res <-开始的位置精确缩进。
标签: list haskell io return monads