Basic questions in ocaml

Hi,
First, i cannot understand what this mean and where we can use these expressions;
let a = None
let b = Some 10 what some mean ?
let b=10
let c = Some true
let d = Some "ok"
Second,
how can write this in ocaml
let matchfunction x y z= match x y z with | | with the condition that function has type int->int->int-> int=

thank you

Some is a constructor of the option type. The definition is roughly:

type 'a option = Some of 'a | None

Which means that it either holds a value of some unspecified ('a) type (Some) or it is empty (None).

So Some 10 is an int option, the Some true is a bool option and the Some "ok" is a string option.

If you want to write a function int -> int -> int -> int you need to create a function which takes 3 arguments, uses them as ints and returns another int. Then the type checker will be able to resolve 'a -> 'b -> 'c -> 'd (the most generic signature) to ints.

1 Like

Hi Rana, would you like some suggestions about where to find good learning resources for OCaml or ML-style functional programming?

2 Likes

There are some useful links in the About the Learning category post. Feel free to suggest other things (though maybe we shouldn’t make it too long).

yes, please and thank you

OK. The best resource I can point you to is Prof. Dan Grossman’s free online course, ‘Programming Languages’: https://www.coursera.org/learn/programming-languages

It has fantastic, short and to the point lecture videos that explain all the concepts really nicely. I think you will enjoy it.

Note that the language used is actually a very close relative of OCaml, called Standard ML; but the concepts are all the same.

2 Likes

beginners question. But how do you extract the value if it does have a value?

I tried this but obviously it shouldn’t work and it doesn’t but it portrays what I was going for:

(* Which means that it either holds a value of some unspecified ('a) type (Some) or it is empty (None). *)
type 'a option =
  | Some of 'a
  | None;;

let some_ten =  Some 10;;

let get_option some_val =
  match some_val with
    | Some a -> a
    | None -> None;;

let ten = get_option some_ten;;

What do you expect the type of the resulting function to be? The first case (Some a -> a) suggests that it should be 'a, but the second case suggests that it should be 'a option. But it’s not allowed to be both.

There are (at least) a few options here:

  1. Have your function take a default value of type 'a to use in the None case. The stdlib function Option.value does this.
  2. Have your function raise an exception in the None case.
  3. Instead of using a function to extract the value, in some cases you might want to do very different things depending on whether the value is present. So the code that uses the 'a value goes in the Some case, and you write different code that doesn’t depend on the value in the None case.

is there no easy way to extract the value from Some constructor? like “indexing”?

I’m not sure I follow. I’ve described three ways of getting the value from an option. But I’m happy to elaborate or give examples, or maybe you could say more about what you’re trying to do.

Let’s rework your original function a bit:

let get_option some_val = match some_val with
  | Some a -> a
  | None -> raise Not_found

This is Levi’s item #2. This throws an exception at runtime if the option argument is actually None. If you’re OK with that, then sure, that works. It’s analogous to ‘indexing’ in some languages where e.g. if you do arr[i] and arr has no element at that index then it throws a runtime exception.

If you’re not OK with that (and many people aren’t because they’re trying to eliminate runtime exceptions as much as possible), then you could give the function a ‘default’ parameter:

let get_option default some_val = match some_val with
  | Some a -> a
  | None -> default

This is Levi’s item #1.

Most notably option #1 is a great way to emulate NullPointerExceptions known and loved from other languages.

The whole point of using options is to be forced to handle both cases and not say “ahh, this is unlikely” and then never handle it.