F#尾部。头部加列表
我正试图编写一些用于处理多项式的F#代码,作为其中的一部分,我想将列表中的重复元素合并到单个元素中。以下是相关代码:F#尾部。头部加列表,f#,list,F#,List,我正试图编写一些用于处理多项式的F#代码,作为其中的一部分,我想将列表中的重复元素合并到单个元素中。以下是相关代码: type PolynomialElem(Coeff : double, Power : int) = member x.Coeff = Coeff member x.Power = Power let rec removeDuplicates (inlist:list<PolynomialElem>) (outlist:list<PolynomialEl
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))
|[] -> []
此外,编译器对我连接列表的方式感到不满意,并说:
This expression was expected to have type PolynomialElem list but here has type PolynomialElem list -> PolynomialElem list
有什么帮助吗?以下是编译的代码:
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))
|[] -> []
类型多项式lem(系数:双精度,幂:整数)=
成员x.系数=系数
成员x.权力=权力
让rec移除副本(inlist:list)=
在列表中匹配
|head::tail->如果head.Power=tail.head.Power,则
多项式lem(head.Coeff+tail.head.Coeff,head.Power)::移除复制tail.tail
其他的
头::(拆下两个(尾))
|[] -> []
您忘记了传递给polynomialem的第二个参数(幂)
您有一些未使用/不需要的“outlist”参数
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))
|[] -> []