Introduction to OCaml Map
In OCaml, we can use the map function with the list library we have already; this function is the part of the list in OCaml so that it can be called on the list. This function takes two arguments as the parameter and bit tricky to understand as well. By the use of 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 needs to be understood before using it.
('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.
let function variable_name = some_operation in List.map any_function [your list argument ];;
How Map Function Work in OCaml?
As we already know, that map function is a bit tricky to understand, this function takes two arguments as the input param, but they have their own significance that we will see later. Here we will see the function signature, which is given by the OCaml official documentation and then a piece of code to understand the internal working.
(‘a -> ‘b) -> ‘a list -> ‘b
As you can see in the above method signature given by the OCaml official documentation, this function responsible to take a function as the argument, which would be 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 we will be a list type in OCaml. But here, this list is the type of the second type here, but the element would be of type a here.
Map function in OCaml is responsible for returning us the list. We will always have list type as the return value from the map function in OCaml. To print it, we have to use some other functions that are already available in the List. Also, it is an in built function in OCaml that means we do not require to include any dependency for this; it is already there by default in OCaml.
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 that we have passed already. So first, we have created the variable names ‘m’, which we will double; after that, we have 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 that will be manipulated by it. The original list is not yet impacted. After this definition, we have used the iter function from the List module of the OCaml, responsible to iterate the list element one by one lie in any other programming language. Also, we have used the print function here to print the elements of the list obtained by the map function as a result.
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.
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;;
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.
This is a guide to OCaml Map. Here we discuss the introduction, how map function work in OCaml? And example, respectively. You may also have a look at the following articles to learn more –