代码之家  ›  专栏  ›  技术社区  ›  John Palmer

F尾。头加列表

  •  -1
  • John Palmer  · 技术社区  · 15 年前

    我正在尝试编写一些用于操作多项式的F代码,作为其中的一部分,我希望将列表中的重复元素组合为单个元素,下面是相关代码:

    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"
    

    另外,编译器对我连接列表的方式也不满意,说:

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

    有什么帮助吗?

    1 回复  |  直到 10 年前
        1
  •  1
  •   Brian    15 年前

    以下是编译的代码:

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

    你忘了传递给多项式的第二个参数(幂)

    您有一些未使用/不需要的“离群值”参数。

    推荐文章