Haskell - alternating elements from two lists

前端 未结 5 649
小鲜肉
小鲜肉 2021-02-07 12:51

I\'m trying to write a haskell function that takes in two lists of integers and generates a list with elements that have been taken alternatingly from the two lists.

I

5条回答
  •  甜味超标
    2021-02-07 13:44

    If you want to zip, generate lists instead of tuples:

    concat $ zipWith (\x y -> [x,y]) [1,2,3] [4,5,6]
    

    Some pointless fun:

    concat $ zipWith ((flip(:)).(:[])) [1,2,3] [4,5,6]  
    

    Probably the easiest way:

    import Data.List
    concat $ transpose [[1,2,3],[4,5,6]]
    

提交回复
热议问题