Split list into two equal lists in F#

后端 未结 7 421
遇见更好的自我
遇见更好的自我 2021-01-12 18:21

I\'m really new to F#, and I need a bit of help with an F# problem.

I need to implement a cut function that splits a list in half so that the output would be...

7条回答
  •  伪装坚强ぢ
    2021-01-12 19:15

    You are looking for list slicing in F#. There was a great answer by @Juliet in this SO Thread: Slice like functionality from a List in F#

    Basically it comes down to - this is not built in since there is no constant time index access in F# lists, but you can work around this as detailed. Her approach applied to your problem would yield a (not so efficient but working) solution:

    let gencut(n, list) = 
        let firstList = list |> Seq.take n |> Seq.toList
        let secondList = list |> Seq.skip n |> Seq.toList
        (firstList, secondList)
    

提交回复
热议问题