2009-10-22 47 views
-1

我想寫操縱多項式一些F#代碼,這部分我想列表重複的元素結合到這裏一個單一的元素是相關代碼:F#tail.Head加上名單

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" 

而且編譯器是不滿的方式串聯IM名單,他說:

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

有什麼幫助嗎?

回答

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

你忘了第二個參數(功率)傳遞到PolynomialElem

你得的是不使用/需要一些「OUTLIST」參數。

+0

謝謝,這工作 – 2009-10-22 07:31:16