Updated June 3, 2023
Introduction to OCaml Map
In OCaml, we can use the map function with the list library we already have; this function is part of the list in OCaml, so it can be called on the list. This function takes two arguments as the parameter and is a bit tricky to understand. Using this function, we can map different values together, and it will always return us the list type as the result of the function. This function has a valid method signature, which must be understood before use.
Syntax:
('a -> 'b) -> 'a list -> 'b list
As you can see in the above line of syntax, we have the map function syntax or definition given by the OCaml official documentation.
Example:
let function variable_name = some_operation in List.map any_function [your list argument ];;
How does Map Function Work in OCaml?
As we already know, the map function is a bit tricky to understand, this function takes two arguments as the input param, but they have their significance that we will see later. Here we will see the function signature, given by the OCaml official documentation, and then a piece of code to understand the internal working.
Method Signature:
(‘a -> ‘b) -> ‘a list -> ‘b
As you can see in the above method signature given by the OCaml official documentation, this function is responsible for taking a function as the argument, mainly of type ‘a’ to type ‘b’ here, as you can see in the method signature already. This function is responsible for returning us the result, which will be a list type in OCaml. But here, this list is the type of the second type, but the element would be of type a here.
Return Type:
The map function in OCaml is responsible for returning the list. We will always have a list type as the return value from the map function in OCaml. We have to use other functions already available in the List to print it. Also, it is an in-built function in OCaml, so we do not require to include any dependency for this; it is already there by default in OCaml.
Example:
Code:
let a = let double m = m * 2 in List.map double [10; 20; 30; 40; 50];;
List.iter print_int a;;
Above is the basic example in OCaml to understand the map function internally. Here we are trying to use the map function to double the values we have already passed. So first, we created the variable name ‘m’, which we will double; after that, we used the List.map function and passed the list of arguments there. Apart from this, we have used the ‘double’ function, which is responsible for doubling every element of the list and returning us the list of elements it will manipulate. The original list is not yet impacted. After this definition, we used the iter function from the List module of the OCaml, which is responsible for iterating the list element one by one in any other programming language. Also, we have used the print function here to print the list elements obtained by the map function.
Example of OCaml Map
Given below is the example of the OCaml Map:
In this example, we are trying to use the map function with several lists we have created and trying to get a different result with the help of parameters we have passed.
Code:
print_string "Demo to show wokring of map function in Ocaml \n";;
print_string "using the function here !!\n";;
let a1 = let double m1 = m1 * 1 in List.map double [10; 20; 30; 40; 50];;
let a2 = let double m2 = m2 * 2 in List.map double [1; 2; 3; 4; 5; 6;];;
let a3 = let double m3 = m3 * 3 in List.map double [15; 25; 35; 45; 55];;
let a4 = let double m4 = m4 * 4 in List.map double [100; 200; 300; 400; 500; 600];;
let a5 = let double m5 = m5 * 5 in List.map double [11; 12; 13; 14; 15; 16];;
let a6 = let double m6 = m6 * 6 in List.map double [8; 9; 7; 5; 6; 7];;
let a7 = let double m7 = m7 * 7 in List.map double [1; 2; 3; 4; 5; 6; 7; 8];;
print_string "printing the result after calculation \n";;
print_string " result one is \n";;
List.iter print_int a1;;
print_string " result two is \n";;
List.iter print_int a2;;
print_string " result three is \n";;
List.iter print_int a3;;
print_string " result four is \n";;
List.iter print_int a4;;
print_string " result five is \n";;
List.iter print_int a5;;
print_string " result six is \n";;
List.iter print_int a6;;
print_string " result seven is \n";;
List.iter print_int a7;;
Output:
Conclusion
In the map function, we have to pass the function as the argument, which will apply the logic and produce the list result. It is easy to use and handle by the developers. Not a requirement of any external library as well.
Recommended Articles
We hope that this EDUCBA information on “OCaml Map” was beneficial to you. You can view EDUCBA’s recommended articles for more information.