Polymorphism in OCaml - ad hoc,parametric, inclusion/subtyping

后端 未结 3 1963
鱼传尺愫
鱼传尺愫 2021-02-10 08:46

I am having a problem understanding the different types of polymorphism, specifically in regards to OCaml. I understand that polymorphism allows for multiple types in OCaml den

3条回答
  •  被撕碎了的回忆
    2021-02-10 09:43

    ident is polymorphous :

    # let ident x = x;;
    val ident : 'a -> 'a = 
    
    # ident 1;;
    - : int = 1
    # ident "ok";;
    - : string = "ok"
    # ident [];;
    - : 'a list = []
    # ident List.length;;
    - : '_a list -> int = 
    # ident ident;;
    - : '_a -> '_a = 
    

    fold also :

    # open List;;
    # fold_left (+) 0 [1;2;3];;
    - : int = 6
    # fold_left (^) "" ["1";"2";"3"];;
    - : string = "123"
    # fold_left (fun a (x,y)  -> a+x*y) 0 [(1,2);(3,4);(5,6)];;
    - : int = 44
    

提交回复
热议问题