开发者

F# tail.Head plus lists

I am trying to write some F# code for manipulating polynomials, as part of that I want to combine duplicate elements of a list into a single element here is the relevant code:

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 produces two different sets of errors:

The head.Coeff + tail.head.Coeff produces a type mismatch saying "type double * int doesn't match type double"

Also the compiler is unhappy about the way im concatenating the lists, saying:

This expression was expected to have type PolynomialElem list but here has type PolynomialElem list -> PolynomialElem list      

Any help?


Here's code that compiles:

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))
        |[] -> []      

You forgot the second param (Power) passed to PolynomialElem

You had some 'outlist' parameter that was not used/needed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜