Split list into two equal lists in F#

后端 未结 7 470
遇见更好的自我
遇见更好的自我 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:14

    I have the same Homework, this was my solution. I'm just a student and new in F#

    let rec gencut(n, listb) = 
        let rec cut  n (lista : int list)  (listb : int list) =
            match (n , listb ) with
            | 0, _   ->  lista, listb
            | _, []  -> lista, listb
            | _, b :: listb -> cut  (n - 1) (List.rev (b :: lista ))  listb
        cut n [] listb
    
    let cut xs = gencut((List.length xs) / 2, xs)  
    

    Probably is not the best recursive solution, but it works. I think

提交回复
热议问题