Basic questions in ocaml


#1

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


#2

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.


#3

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


#4

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).


#5

yes, please and thank you


#6

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.