我正在尝试编写一些用于操作多项式的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
任何帮助?
答案 0 :(得分:1)
这是编译的代码:
type PolynomialElem(Coeff : double, Power : int) =
member x.Coeff = Coeff
member x.Power = Power
let rec removeDuplicates (inlist:list<PolynomialElem>) =
match inlist with
|head:: tail ->if head.Power = tail.Head.Power then
PolynomialElem(head.Coeff + tail.Head.Coeff, head.Power) :: removeDuplicates tail.Tail
else
head :: (removeDuplicates(tail))
|[] -> []
你忘记了第二个参数(Power)传递给PolynomialElem
您有一些未使用/需要的'outlist'参数。