Haskell - alternating elements from two lists

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

    I will assume that this is homework. Provided that you can create the following list (as you said):

    [(1,4),(2,5),(3,6)]
    

    ... you can solve it with 2 functions:

    1. You need to convert a tuple (a, b) into a list [a, b]. Try using pattern matching! This function needs to be applied (aka. mapped) over all elements of the list you have.
    2. You will have a list of lists, like [[1,4],[2,5],[3,6]], so you need a function for concatenating the sublists into one big list.

    There are of course other, maybe superior, ways to solve this problem, but it might be a good idea to continue with your original approach.

提交回复
热议问题