我刚刚开始使用F#,所以这可能是一个微不足道的问题,但我无法理解为什么我的代码中的模式匹配就像它一样行事。代码的F#模式匹配出错了
快速解释:
的FUNC calcNextMatch应该递归一个列表,如果2个元素是相等的它们应该被加在一起。 最后,func应该返回一个数字,即添加与列表中下一位数字匹配的所有数字。
f.ex. [1; 3; 2; 2; 5]应返回4
代码:
let rec printList l =
match l with
| head :: tail -> printf "%d " head; printList tail
| [] -> printfn ""
let rec calcNextMatch list =
printList list
match list with
| [] -> 0
| _ :: tail ->
printList tail
let h = Seq.head list
let t = Seq.tryHead tail
printfn "h: %i" h
printfn "t: %O" t
match t with
| Some h ->
printfn "TAIL t: %i is equal to HEAD h: %i" t.Value h
printfn "Calculation is: %i" (t.Value + h)
(t.Value + h) + calcNextMatch tail
| _ -> calcNextMatch tail
let sequence = [ 1;3;2;2;5 ]
let run = calcNextMatch sequence
当运行该代码的问题是,模式匹配 如我期望它不起作用。从运行脚本f.ex f.ex打印输出。
h: 1
t: Some(3)
TAIL t: 3 is equal to HEAD h: 3
这意味着F#已在的情况下匹配
match t with
| Some h ->
其中t =一些(3)和h = 1 其转换为
match 3 with
| Some 1 ->
和我不理解。 匹配状态之前打印吨和h的给和但在值的模式匹配的ħ值已更改为
这是如何实现?
'某些h'声明了一个新的变量'h',与现有变量的值不匹配。 – Mankarse