OCaml - Function - Creating a getter to retrieve an element of a tuple

OK, this is not really a getter implementation like we can have it in other object-oriented programming, but it is close of it.

We have first to create an human variable with a tuple ("name", age).
Then we have to create two getter functions to retrieve the first and the second parameter of this human variable.

Let's begin by the human variable:
# let human = ("John", 50);;

val human : string * int = ("John", 50)
Let's continue by the getName function:
# let getName(a,b)=a;;

val getName : 'a * 'b -> 'a = <fun>
Do the same for the getAge function:
# let getAge(a,b)=b;;

val getAge : 'a * 'b -> 'b = <fun>
Finally we can reach each of elements in tuple of the human variable.
For example we can get the name of it:
# getName(human);;

- : string = "John"
Great job, you made it! laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.