How to write code in F# for what functors do in OCaml?

后端 未结 3 2256
一个人的身影
一个人的身影 2021-02-19 19:21

I have many programs written in OCaml, some of them use functors. Now, I am considering of writing and re-writing a part of code in F# (to benefit some advantages that OCaml doe

3条回答
  •  借酒劲吻你
    2021-02-19 20:22

    The functional way in F# would rely mostly on type inference avoiding OOP structures like interface or types with member.

    type Comparison = Less | Equal | Greater
    type OrderedSet<'t> = 't list     // type alias, not really necessary
    
    module OrderedSet =
        let empty                      : OrderedSet<_> = List.empty  // just an empty list
        let values (s : OrderedSet<_>) : OrderedSet<_> = s           // identity function
        let add compare x (s : OrderedSet<_>)  : OrderedSet<_> =
            let rec addR s =
                match s with
                | [] -> [x]
                | hd::tl ->
                    match compare x hd with
                    | Equal   -> s         (* x is already in s *)
                    | Less    -> x :: s    (* x is smaller than all elements of s *)
                    | Greater -> hd :: addR tl
            addR s
    
        let compare        x          y = if x = y then Equal else if x < y then Less else Greater
        let compareFloats (x : float) y = if x = y then Equal else if x < y then Less else Greater
    
        let addGeneric v = add compare       v
        let addFloat   v = add compareFloats v
    

    And it is used like this:

    let try1 () = OrderedSet.addGeneric "foo" OrderedSet.empty |> OrderedSet.addGeneric "bar"
    let try2 () = OrderedSet.addGeneric 2     OrderedSet.empty |> OrderedSet.addGeneric 3 
    let try3 () = OrderedSet.empty 
                  |> OrderedSet.addFloat 3.0 
                  |> OrderedSet.addFloat 1.0 
                  |> OrderedSet.addFloat 2.0
    
    try1() |> printfn "%A"  // OrderedSet = ["bar"; "foo"]  
    try2() |> printfn "%A"  // OrderedSet    = [2; 3]
    try3() |> printfn "%A"  // OrderedSet  = [1.0; 2.0; 3.0]
    

    The type alias type OrderedSet<'t> = 't list and the functions empty and values are not really necessary but they help to mask the actual implementation (in case that is desirable).

提交回复
热议问题