Suppose you have a list of key/value pairs (i.e., a nested list where each element of the list is a (key value)pair. For example, the list might look like this: '((France Paris) (France Nice) (Spain Madrid) (Poland Krakow) (Poland Warsaw)) (Notice that the keys need not be unique for this problem.) Write a function named first-occurrence in Scheme that takes a key and a list then returns the value of the first item on the list with a matching key. Return '() if none of items on the list has a matching key.
Suppose you have a list of key/value pairs (i.e., a nested list where each element of the list is a (key value)pair. For example, the list might look like this: '((France Paris) (France Nice) (Spain Madrid) (Poland Krakow) (Poland Warsaw)) (Notice that the keys need not be unique for this problem.)
Write a function named first-occurrence in Scheme that takes a key and a list then returns the value of the first item on the list with a matching key. Return '() if none of items on the list has a matching key.
You must use either fold or reduce to do the vast majority of the work. You may use the built-in foldmethod by adding the line (require 'list-lib), or you can use your own implementation.
The built-in fold function has parameters in this order: action partial-result lst.
You may assume that the list contains an element with the key to be updated.
For example,
- (first-occurrence 'Germany '((France Paris) (Germany Bonn) (Germany Berlin))) should return 'Bonn
- (first-occurrence 'Poland '((France Paris) (Germany Bonn) (Germany Berlin))) should return '()
- (first-occurrence 'Poland '()) should return '()
Step by step
Solved in 3 steps