tags:

views:

116

answers:

1

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 to 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?

+2  A: 

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.

Brian
thanks, this worked
jpalmer