先前用F#的误点。F#相较C#,真正强大的是函数式编程,而c#是面向对象的编程,写代码时,c#面对的是变量,而F#面对的是函数,好的F#代码尽量少的声明一个名称关联到值。这样在执行代码时可以尽量少的占用空间,从而提高效率,这是提高效率的主要地方,谨记!!!!

静态方法:

static member GetContacts (lstContact:(int * int) seq) (key:int)  = 
()

静态私有方法

static member private GetContacts (lstContact:(int * int) seq) (key:int)  = 
()

 继承接口

type DivDetailAna(divDetail:DivDetail, anaDetails : Set<DivPAnaBase>) =
    interface System.IComparable with
        override this.CompareTo(other) = 
            match other with
            | :? DivDetailAna as oth -> (this.DivDetail.P * (-1)).CompareTo(oth.DivDetail.P * (-1))
            | _ -> 1

继承类

type DivPAnaComplexUnit(paper:LayoutResoultComplex, machine:int, cost:float, costID:int) =
    inherit DivPAnaBase(paper.ColumnsCount * paper.RowsCount ,machine, cost, costID)

记录:

类具有引用相等性语义。

记录的定义:字段 + 成员

type OptionAA = 
    {
        ID: int;
        Name:string;
        mutable Text:string;
    }
    member x.AAA() = printfn "%A" x.Text
let test = {ID = 0; Name = "AA"; Text = "YYY"}
test.Text <- "ZZZ"
printfn "%A" (test.AAA())

 可区分联合:

此外,递归的可区分联合用于表示树数据结构。

type type-name =
   | case-identifier1 [of type1 [ * type2 ...]
   | case-identifier2 [of type3 [ * type4 ...]
   ...

比较普遍的用法,二叉树。

type Tree =
    | Tip
    | Node of int * Tree * Tree

let rec sumTree tree =
    match tree with
    | Tip -> 0
    | Node(value, left, right) ->
        value + sumTree(left) + sumTree(right)
let myTree = Node(0, Node(1, Node(2, Tip, Tip), Node(3, Tip, Tip)), Node(4, Tip, Tip))
let resultSumTree = sumTree myTree

 

相关文章:

  • 2021-08-13
  • 2021-09-26
  • 2022-02-23
  • 2021-05-05
  • 2021-09-02
  • 2021-06-10
  • 2021-06-14
猜你喜欢
  • 2021-08-02
  • 2021-10-16
  • 2022-01-21
  • 2021-04-29
  • 2022-12-23
  • 2021-07-06
  • 2021-08-11
相关资源
相似解决方案