How to resolve the algorithm Search a list of records step by step in the Scheme programming language
How to resolve the algorithm Search a list of records step by step in the Scheme 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 Scheme programming language
Source code in the scheme programming language
(import (scheme base)
(scheme char)
(scheme write)
(srfi 1) ; lists
(srfi 132)) ; sorting
(define-record-type <places> ; compound data type is a record with two fields
(make-place name population)
place?
(name place-name)
(population place-population))
(define *items*
(list-sort ; sort by decreasing population
(lambda (r1 r2) (> (place-population r1)
(place-population r2)))
(list (make-place "Lagos" 21.0)
(make-place "Cairo" 15.2)
(make-place "Kinshasa-Brazzaville" 11.3)
(make-place "Greater Johannesburg" 7.55)
(make-place "Mogadishu" 5.85)
(make-place "Khartoum-Omdurman" 4.98)
(make-place "Dar Es Salaam" 4.7)
(make-place "Alexandria" 4.58)
(make-place "Abidjan" 4.4)
(make-place "Casablanca" 3.98))))
;; Find the (zero-based) index of the first city in the list
;; whose name is "Dar Es Salaam"
(display "Test 1: ")
(display (list-index (lambda (item)
(string=? "Dar Es Salaam" (place-name item)))
*items*))
(newline)
;; Find the name of the first city in this list
;; whose population is less than 5 million
(display "Test 2: ")
(display (place-name
(find (lambda (item)
(< (place-population item) 5.0))
*items*)))
(newline)
;; Find the population of the first city in this list
;; whose name starts with the letter "A"
(display "Test 3: ")
(display (place-population
(find (lambda (item)
(char=? (string-ref (place-name item) 0)
#\A))
*items*)))
(newline)
You may also check:How to resolve the algorithm Euler's sum of powers conjecture step by step in the J programming language
You may also check:How to resolve the algorithm Perfect shuffle step by step in the Scilab programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Knight's tour step by step in the Bracmat programming language
You may also check:How to resolve the algorithm Peripheral drift illusion step by step in the Perl programming language