Haskell - alternating elements from two lists

前端 未结 5 647
小鲜肉
小鲜肉 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:45

    How about exchanging the arguments during recursion-descend?

    blend (x:xs) ys = x:(blend ys xs)
    blend _ _ = []
    

    You can even generalise this approach for any number of lists (I'll leave this to you) or take the remaining elements of a list if the other is empty:

    blend _ ys = ys
    

提交回复
热议问题