|
|
1
11
单子的魔力: 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] 这可能需要更多的解释。 你可以自己写
这样的话
然而,
comprehensions
和
monads
有意相似。标准序曲有
作为另一条捷径,
对于每个基的不同幂的内部列表,您需要将它们乘以一个数字。你可以递归地写这个
或者使用折叠
但实际上,
|