列表 monad 的魔力:
ghci> let powers (a, b) = [a ^ n | n <- [0 .. b-1]]
ghci> powers (2, 3)
[1,2,4]
ghci> map powers [(2, 3), (5, 3)]
[[1,2,4],[1,5,25]]
ghci> sequence it
[[1,1],[1,5],[1,25],[2,1],[2,5],[2,25],[4,1],[4,5],[4,25]]
ghci> mapM powers [(2, 3), (5, 3)]
[[1,1],[1,5],[1,25],[2,1],[2,5],[2,25],[4,1],[4,5],[4,25]]
ghci> map product it
[1,5,25,2,10,50,4,20,100]
ghci> let allPowers list = map product $ mapM powers list
ghci> allPowers [(2, 3), (5, 3)]
[1,5,25,2,10,50,4,20,100]
这可能值得更多解释。
你可以自己写
cartesianProduct :: [[a]] -> [[a]]
cartesianProduct [] = [[]]
cartesianProduct (list:lists)
= [ (x:xs) | x <- list, xs <- cartesianProduct lists ]
这样cartesianProduct [[1],[2,3],[4,5,6]]
⇒ [[1,2,4],[1,2,5],[1,2,6],[1,3,4],[1,3,5],[1,3,6]]
.
然而,理解力 http://www.haskell.org/onlinereport/exps.html#list-comprehensions and monads http://www.haskell.org/haskellwiki/List_comprehension#List_monad故意相似。标准 Prelude 有sequence :: Monad m => [m a] -> m [a] http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Asequence, 什么时候m
是列表单子[]
,它实际上完全按照我们上面写的那样操作。
作为另一个捷径,mapM :: Monad m => (a -> m b) -> [a] -> m [b] http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3AmapM只是一个组合sequence
and map
.
对于每个底数的不同幂的每个内部列表,您希望将它们乘以一个数字。你可以递归地写这个
product list = product' 1 list
where product' accum [] = accum
product' accum (x:xs)
= let accum' = accum * x
in accum' `seq` product' accum' xs
或使用折叠
import Data.List
product list = foldl' (*) 1 list
但实际上,product :: Num a => [a] -> a http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aproduct已经定义了!我喜欢这门语言☺☺☺