【发布时间】:2017-08-15 01:05:28
【问题描述】:
我对 F# 和函数式编程非常陌生,我想递归地创建一个函数,该函数接受一个列表,并将所有元素加倍。
这是我用来搜索空间元素的方法,但我不确定如何准确地更改它来满足我的需要。
let rec returnN n theList =
match n, theList with
| 0, (head::_) -> head
| _, (_::theList') -> returnN (n - 1) theList'
| _, [] -> invalidArg "n" "n is larger then list length"
let list1 = [5; 10; 15; 20; 50; 25; 30]
printfn "%d" (returnN 3 list1 )
有没有办法让我增强它来做我需要做的事情?
【问题讨论】:
-
提问时,您应该始终展示您是如何尝试解决问题的。就您的问题而言,您基本上只是要求某人为您解决问题。
标签: list function recursion f# functional-programming