这是一个非常简单的问题,但我只是不熟悉模式匹配和f#语法。
我正在尝试编写一个函数,它接受一个int和一个‘list’,并返回a类型的第n个元素。我在想这样的事情:
let rec getn n xs=
match n with
0 -> {split xs into x::xs and then return x}
_ -> {split xs into x::xs and call getn with n-1, xs}有人能帮我处理一下语法吗?谢谢
发布于 2015-01-26 00:35:14
let rec getn n xs =
match n, xs with
| 0, (x::_) -> x
| _, (_::xs') -> getn (n - 1) xs'
| _, [] -> invalidArg "n" "n is too large"不过,我认为if会比match更清楚:
let rec getn n xs =
if List.isEmpty xs then invalidArg "n" "n is too large"
let x::xs' = xs in if n = 0 then x else getn (n - 1) xs'https://stackoverflow.com/questions/28143186
复制相似问题