2009-10-22 7 views
-1

Je suis en train d'écrire un code F # pour Manipulations de polynômes, dans le cadre de ce que je veux combiner des éléments en double d'une liste en un seul élément est ici le code correspondant:F # tail.Head ainsi que des listes

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

Cela produit deux séries d'erreurs différentes:

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

de plus, le compilateur est mécontent de la façon dont im concaténer les listes, en disant:

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

Toute aide?

Répondre

1

Code est ici compilant:

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

Vous avez oublié le deuxième param (Power) est passé à PolynomialElem

Vous avez eu un paramètre 'outlist' qui n'a pas été utilisé/nécessaire.

+0

merci, cela a fonctionné –

Questions connexes