OCaml - What data type is not and is not?

If I use Some and None combos in a list, what will be the data type of the list? Is it always 'a ? Or is there some type for Some / None ?

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

If I put int , it will give me an error:

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

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

It is not clear why polymorphic functions are useful, but they are very useful and very common, so we will discuss them later. (Hint: polymorphism is similar to patterns in C ++ or generics in Java 1.5).

I thought this looked like a link / pointer in other languages, which actually makes sense. But now I really don’t understand what type of None . The same thing happens with Some .

In addition, I know that I have to ask two questions in one question, but this one has a strong relation to the previous question. What is the meaning of some? I usually see that it is used when None is used. If I implement the above list without Some, it still compiles, but the list structure does not have the “option” flag, which I assume is optional (I cannot find anything on the Internet about this). Can someone provide me with one case, is this useful?

+6
source share
4 answers

What you wrote here is a value of the type (int option * int option * 'a option * int option) list , that is, a list of quadruplets in which the first two components are optional integers, the next is still polymorphic (in undefined) and the latter is an optional integer. This is due to the fact that it is a tuple delimiter. List separator , so I think you wanted to write

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

Who has the 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 .

+8
source

Caml has the predefined type `a option ( `a means that it can be a variant of anything, just like `a list can be int list , a float list ...). This is actually a very simple type:

 type `a option = Some of `a | None 

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

You should probably think of it as an explicit way to deal 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 is of type `a list -> `a , which allows the caller to forget about the empty case of the list. The second type is of type `a list -> `a option , which means that the caller will always handle the error.

In other words: None somewhat similar to null , except that you can always tell from the type of function whether it can return None (and if possible, the compiler forces you to handle this).

+2
source

This is an option type indicating whether it has any value or not.

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

+1
source

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 solution for the code (which 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 
0
source

All Articles