首页 > 解决方案 > 列表的递归除法 F#

问题描述

看来,我的递归函数需要帮助,它没有应用。我有一个例子,但我不明白我必须做什么。我的错误在哪里?我很乐意为您提供任何帮助!

我的功能:

let rec split = function
  | [] -> ([],[])
  | [x] -> ([x], [])
  | x :: y :: tail -> split ((fun z -> z)(fun acc -> (x :: fst acc, y :: snd acc))) tail
let res = split [1; 2; 3; 4; 5]
printfn "%A" res 

例子:

let rec split cont = function
  | [] -> cont ([],[])
  | [x] -> cont ([x], [])
  | x :: y :: tail -> split (fun acc -> cont (x :: fst acc, y :: snd acc)) tail
let res = split (fun x -> x) [1; 2; 3; 4; 5]
printfn "%A" res 

标签: f#

解决方案


我找到了...

let rec split = function
  | x :: y :: xs -> let (xs1, xs2) = split xs in (x :: xs1, y :: xs2)
  | [x] -> ([x], [])
  | [] -> ([],[])
// let res = split [1; 2; 3; 4; 5]
// printfn "%A" res

推荐阅读