我正在做一些关于F#的练习,我有一个函数来计算备用和:
let rec altsum = function
| [] -> 0
| [x] -> x
| x0::x1::xs -> x0 - x1 + altsum xs;;
val altsum : int list -> int
这个练习包括声明同一个函数,只使用两个clauses...but,如何做到这一点?
发布于 2015-06-04 15:55:47
我的教条盒的答案是正确的和工作的!
但经过几次尝试,我找到了这个问题的最小和可读性的解决方案。
let rec altsum2 = function
| [] -> 0
| x0::xs -> x0 - altsum2 xs
示例
altsum2 [1;2;3] essentially do this:
1 - (2 - (3 - 0)
这是有点棘手,但工作!
OFF主题:
使用F#列表库解决问题的另一种优雅方法是:
let altsum3 list = List.foldBack (fun x acc -> x - acc) list 0;;
在phoog的评论之后,我开始尝试用尾递归函数来解决这个问题:
let tail_altsum4 list =
let pl l = List.length l % 2 = 0
let rec rt = function
| ([],acc) -> if pl list then -acc else acc
| (x0::xs,acc) -> rt (xs, x0 - acc)
rt (list,0)
这也有点像tricky...substraction是不可交换的,不可能用List.rev
(一个长的list...but,我找到了一个解决办法!)
发布于 2015-06-04 15:44:24
为了减少案例的数量,您需要将算法移回更接近原始问题的位置。问题是否定交替值,所以这就是你的解决方案应该做的。
let altsum lst =
let rec altsumRec lst negateNext =
match lst with
| [] -> 0
| head::tail -> (if negateNext then -head else head) + altsumRec tail (not negateNext)
altsumRec lst false
https://stackoverflow.com/questions/30647694
复制相似问题