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

前端 未结 8 2096
悲&欢浪女
悲&欢浪女 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条回答
  •  旧时难觅i
    2020-12-11 01:43

    Just for completeness here is a boring, more imperative solution:

    let splitList (list:int list) =
        let odds = [for i in 0..list.Length-1 do
                      if i%2=1 then
                        yield list.[i]]
        let evens = [for i in 0..list.Length-1 do
                        if i%2=0 then
                            yield list.[i]]
        odds,evens 
    

提交回复
热议问题