如何在F#中声明辅助函数

时间:2015-02-01 04:48:44

标签: f# functional-programming

我试图编写这个函数gencut(n,xs),它将列表xs拆分为2,其中n作为第一个列表的元素数。它是这样的:

let rec gencut(x, ys) = 
    let rec gencutaux (x, xs, ys) = // Here's my problem!
        match (x, ys) with
        | (0, [])  ->  (List.rev xs, ys)
        | (x, [])  ->  (List.rev xs, ys)
        | (0, ys)  ->  (List.rev xs, ys)   
        | (x, y :: ys) -> gencutaux (x - 1, y :: xs,  ys) 
 gencut(4, [1;2;3;4;5]);;  

但是,我在第二行有一个错误。每当我尝试运行它时,控制台会说: "阻止这个'让'尚未完成。期待一个表达。" 我不知道为什么会这样,或者我该怎么做才能纠正它。任何见解都表示赞赏。

1 个答案:

答案 0 :(得分:-1)

您可能想要致电gencutaux

let rec gencut (x, ys) =
    let rec gencutaux (x, xs, ys) =
        match (x, ys) with
        | (0, [])  ->  (List.rev xs, ys)
        | (x, [])  ->  (List.rev xs, ys)
        | (0, ys)  ->  (List.rev xs, ys)
        | (x, y :: ys) -> gencutaux (x - 1, y :: xs,  ys)
    gencutaux (x, xs, [])

您无法使用变量定义结束块,因为这没有任何意义。