Splitting a list of items into two lists of odd and even indexed items

前端 未结 8 2083
悲&欢浪女
悲&欢浪女 2020-12-11 00:56

I would like to make a function that accepts a list and returns two lists: the first contains every odd item, and the second contains every even item.

For example, g

8条回答
  •  暖寄归人
    2020-12-11 01:45

    If you mean odd and even values for positions of items, here is a (non-tail-recursive) solution:

    let rec splitList = function
        | [] -> [], []
        | [x]-> [x], []
        | x1::x2::xs -> let xs1, xs2 = splitList xs
                        x1::xs1, x2::xs2
    

提交回复
热议问题