How to resolve the algorithm Search a list of records step by step in the OCaml programming language
How to resolve the algorithm Search a list of records step by step in the OCaml programming language
Table of Contents
Problem Statement
Many programming languages provide convenient ways to look for a known value in a simple list of strings or numbers. But what if the elements of the list are themselves compound records/objects/data-structures, and the search condition is more complex than a simple equality test? Write a function/method/etc. that can find the first element in a given list matching a given condition. It should be as generic and reusable as possible. (Of course if your programming language already provides such a feature, you can use that instead of recreating it.) Then to demonstrate its functionality, create the data structure specified under #Data set, and perform on it the searches specified under #Test cases. The data structure to be used contains the names and populations (in millions) of the 10 largest metropolitan areas in Africa, and looks as follows when represented in JSON: However, you shouldn't parse it from JSON, but rather represent it natively in your programming language.
If any of that is impossible or unreasonable in your programming language, then feel free to deviate, as long as you explain your reasons in a comment above your solution. If your programming language supports higher-order programming, then the most elegant way to implement the requested functionality in a generic and reusable way, might be to write a function (maybe called "find_index" or similar), that takes two arguments: If this is not the approach which would be most natural or idiomatic in your language, explain why, and show what is.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Search a list of records step by step in the OCaml programming language
Source code in the ocaml programming language
#load "str.cma"
(* We are going to use literally a list of records as said in the title of the
* task. *)
(* First: Definition of the record type. *)
type city = {
name : string;
population : float
}
(* Second: The actual list of records containing the data. *)
let cities = [
{ name = "Lagos"; population = 21.0 };
{ name = "Cairo"; population = 15.2 };
{ name = "Kinshasa-Brazzaville"; population = 11.3 };
{ name = "Greater Johannesburg"; population = 7.55 };
{ name = "Mogadishu"; population = 5.85 };
{ name = "Khartoum-Omdurman"; population = 4.98 };
{ name = "Dar Es Salaam"; population = 4.7 };
{ name = "Alexandria"; population = 4.58 };
{ name = "Abidjan"; population = 4.4 };
{ name = "Casablanca"; population = 3.98 }
]
(* I can't find in the standard library any function in module List that returns
* an index. Well, never mind, I make my own... *)
let find_index pred =
let rec doloop i = function
| [] -> raise Not_found
| x :: xs -> if pred x then i else doloop (i + 1) xs
in
doloop 0
(* List.find returns the first element that satisfies the predicate.
* List.filter or List.find_all would return *all* the elements that satisfy the
* predicate. *)
let get_first pred = List.find pred
(* Simulate the 'startswith' function found in other languages. *)
let startswith sub s =
Str.string_match (Str.regexp sub) s 0
let () =
(* We use a typical dot notation to access the record fields. *)
find_index (fun c -> c.name = "Dar Es Salaam") cities
|> print_int
|> print_newline;
(get_first (fun c -> c.population < 5.0) cities).name
|> print_endline;
(get_first (fun c -> startswith "A" c.name) cities).population
|> print_float
|> print_newline;
You may also check:How to resolve the algorithm Pell numbers step by step in the Scala programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the Visual FoxPro programming language
You may also check:How to resolve the algorithm Fixed length records step by step in the Perl programming language
You may also check:How to resolve the algorithm Loops/Do-while step by step in the Lingo programming language
You may also check:How to resolve the algorithm Totient function step by step in the BCPL programming language