OCaml - What data type is some and none?

谁都会走 提交于 2021-02-07 12:38:52

问题


If I am using Some and None combo in a list what would be the data type of the list? Is it always 'a? Or is there some sort of a type for Some/None?

let listVar : (* type here *) list = [Some 4; Some 3; None; Some 2];;

If I put int it gives me error:

This expression has type int option * int option * 'a option * int option but is here used with type int

When I put 'a it compiles fine but the basic OCaml tutorial says (I made references to other languages to explain my question better):

It won't be clear yet why polymorphic functions are useful, but they are very useful and very common, and so we'll discuss them later on. (Hint: polymorphism is kind of like templates in C++ or generics in Java 1.5).

I thought this was like reference/pointer in other languages, which actually made sense. But now, I really don't understand what type is None. Same goes with Some.

Also, I know I should ask two questions in one question, but this one has a strong relation to the previous question. What is the point of Some? I usually see it used when None is used. If I implement the above list without Some, it still compiles but the list structure doesn't have the "option" flag, which I am guessing means optional (I cant seem to find anything on the internet regarding this). Can someone provide me with one case this is useful?


回答1:


What you have written here is a value of type (int option * int option * 'a option * int option) list, i.e. a list of quadruplets in which the first two component are optional integer, the next one is polymorphic so far (the Some case is undefined yet) and the last one is an optional integer. This is because , is the tuple separator. The list separator is ; so I guess you wanted to write

let listVar = [Some 4; Some 3; None; Some 2];;

Which has type (int option) list. Whenever you use Some or None with an arbitrary value of type 'a you get a value of type 'a option.




回答2:


It is option type, indicating whether it has some value, or not.

If you come from Java or C# or other imperative programming, whenever you want to return a null in a method of Java, in OCaml, you should consider return a None




回答3:


Caml has a predefined `a option type (the `a means it can be an option of anything, in the same way that an `a list can be an int list, a float list...). It's actually a very simple type :

type `a option = Some of `a | None

Basically, a value with type `a option is either None or a value of type `a put inside a box : it is not the same type as `a (which is why you get an error when you tell the compiler to treat it as an int).

You should probably think of it as an explicit way of dealing with errors (more explicit than exceptions). Consider those (non tail recursive, poorly written) functions :

let rec max_exn l = 
  match l with
    | [] -> failwith "Empty list"
    | [x] -> x
    | x::tl -> max x (max_exn tl)

let rec max_option l = 
  match l with
    | [] -> None
    | [x] -> Some x
    | x::tl -> max x (max_option tl) (* max does work on options *)

The first one has type `a list -> `a, which makes it possible for the caller to forget about the empty list case. The second one has type `a list -> `a option, meaning that the error case will always be dealt with by the caller.

To put it another way : None is somewhat similar to null, except you can always tell from the type of a function whether or not it might return None (and if it might, the compiler forces you to deal with it).




回答4:


When I try to compile the solution code from above ( @Jbeuh ), I get the following error message:

Error: This expression has type 'a but an expression was expected of type
         'a option
       The type variable 'a occurs inside 'a option

So I decided to offer an alternative code solution (that works):

let rec max_el = function
    | [] -> None
    | [x] -> Some x
    | x::xs -> let m = max_el xs in
                    if Some x > m then Some x 
                    else m


来源:https://stackoverflow.com/questions/21498796/ocaml-what-data-type-is-some-and-none

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!