我正在尝试编写一些用于操作多项式的F代码,作为其中的一部分,我希望将列表中的重复元素组合为单个元素,下面是相关代码:
type PolynomialElem(Coeff : double, Power : int) =
member x.Coeff = Coeff
member x.Power = Power
let rec removeDuplicates (inlist:list<PolynomialElem>) (outlist:list<PolynomialElem>) =
match inlist with
|head:: tail ->if head.Power = tail.Head.Power then
PolynomialElem(head.Coeff + tail.Head.Coeff) :: removeDuplicates tail.Tail
else
head :: (removeDuplicates(tail))
|[] -> []
这会产生两组不同的错误:
The head.Coeff + tail.head.Coeff produces a type mismatch saying "type double * int doesn't match type double"
另外,编译器对我连接列表的方式也不满意,说:
This expression was expected to have type PolynomialElem list but here has type PolynomialElem list -> PolynomialElem list
有什么帮助吗?