我有一个非常简单的函数,它接受一个整数并将其添加到列表的头部,然后使用i乘以自身递归调用:
let rec f i = function
| [] -> []
| x::xs -> (x+i)::f (i*i) xs
f 2 [1;2;3]
val it : int list = [3; 6; 19]
现在,我正试图使用续延来重写它,但是我有些困惑。以下是我迄今为止想到的:
let fC i l =
let rec loop cont = function
| [] -> []
| x::xs -> cont(x+i)::loop (fun acc -> (acc*acc)) xs
loop id l
fC 2 [1;2;3] //Expected [3;6;19]
val it : int list = [3; 16; 25]
请给我一些提示,我做错了什么吗?
let fC i l = let rec loop cont = function | [] -> [] | x::xs -> x+cont(i)::loop (fun acc -> cont(acc*acc)) xs loop id l
- DevNewb