Haskell List of tuples to list?

后端 未结 5 1605
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-31 13:27

Is it possible to convert a list of tuples [(Int,Int)] as a generic way which valid to any input size ? .. i saw in various questions thats its not possible gen

5条回答
  •  半阙折子戏
    2020-12-31 13:36

    This can be also achieved by the homogeneous tuples library (disclaimer: which I'm the author of). It defines wrappers for tuples that make them instances of Traversable (and others such as Applicative and Monad). So a tuple can be converted to a list by toList . Tuple2 (where toList is from Data.Foldable) and

    f :: [(a, a)] -> [a]
    f = concatMap (toList . Tuple2)
    

    You can also use it for other tuples, for example concatMap (toList . Tuple5) etc.

提交回复
热议问题