Nik*_*s R 0 haskell tuples list-comprehension
有可能以某种方式使用元组作为列表理解的输入吗?或者也许是一个元组理解?我期望以下工作,但事实并非如此.
[x * 2 | x <- (4, 16, 32)]
Run Code Online (Sandbox Code Playgroud)
我不能从一开始就使用列表作为我的作业功能的给定签名
success :: (Int, Int, Int) -> Int -> (Int, Int, Int) -> Bool
Run Code Online (Sandbox Code Playgroud)
但是使用列表会简单得多,因为任务的一部分需要我计算元组中有多少1s和20s.
Control.Lens 已经为所有长度的同类元组重载了遍历支持:
import Control.Lens
-- Convert to list:
(3, 4, 5)^..each -- [3, 4, 5]
(1, 2)^..each -- [1, 2]
-- modify elements:
(4, 16, 32)& each %~ \x -> x * 2 -- (8, 32, 64)
(1, 2)& each %~ (+1) -- (2, 3)
-- operator notation for common modifications (see Control.Lens.Operators):
(1, 2, 3)& each +~ 2 -- (3, 4, 5)
(1, 2, 3)& each *~ 2 -- (2, 4, 6)
-- monadic traversals (here each works like `traverse` for the list monad)
each (\x -> [x, x + 1]) (1, 2) -- [(1,2),(1,3),(2,2),(2,3)]
-- `each` is basically an overloaded "kitchen sink" traversal for
-- common containers. It also works on lists, vectors or maps, for example
[(3, 4), (5, 6)]& each . each +~ 1 -- [(4, 5), (6, 7)]
Run Code Online (Sandbox Code Playgroud)