我试图制作一个利用匹配来创建斐波那契数列的递归函数。我让 Euler2 工作,但在 Euler2a 中,我试图让整个函数自包含,除了迭代约束 j。
Euler2a 必须如何改变才能提供序列?
任何指针都非常感谢。:)
Euler2的结果
val Euler2 : list:int list -> i:int -> j:int -> int list
val z : int list = [1; 2;3;5个;8个;13; 21; 34; 55; 89]
Euler2a的结果
val Euler2a : j:int -> (int list -> int -> int -> int list)
val z2 : (int list -> int -> int -> int list)
欧拉2
let rec Euler2 (list: int list) i j =
match i with
| 1 | 2 -> Euler2 list (i+1) j
| _ when (i<=j) -> let newList = list @ [list.[list.Length - 1] + list.[list.Length - 2]]
Euler2 newList (i+1) j
| _ -> list
let z = Euler2 [1;2] 1 10
欧拉2
let rec Euler2a (j:int) =
let i = 1
let list = [1;2]
let rec Euler2b (list: int list) i j =
match i with
| 1 | 2 -> Euler2b list (i+1) j
| _ when (i<=j) -> let newList = list @ [list.[list.Length - 1] + list.[list.Length - 2]]
Euler2b newList (i+1) j
| _ -> list
Euler2b
let z2 = Euler2a 10