Question

In: Computer Science

Racket/Scheme language [15p] create-mapping A map data structure is described as either a set of key-value...

Racket/Scheme language

[15p] create-mapping A map data structure is described as either a set of key-value pairs or a set of keys and a set of values whose relation is described by a function with a one-to-one mapping from keys to values. Write a function, create-mapping, which takes a list of symbols (keys) and a list of any type of Scheme values (vals) and returns a function that takes one symbol argument (the key) and returns the corresponding value. The mapping is determined by the position of the elements in the two lists.

For example: ;keys > (define roman-numerals-keys '(I II III IV V)) ;values > (define arabic-numerals-vals '(1 2 3 4 5)) ;

The pairs mapped will be: ;I -> 1 ;II -> 2 ;III -> 3 ;and so forth ;the (create-mapping) function returns a function that we bind for later use > (define roman-to-arabic (create-mapping roman-numerals-keys arabicnumerals-vals)) > (roman-to-arabic 'I) 1 > (roman-to-arabic 'V) 5 > (roman-to-arabic 'some-symbol) uncaught exception: "Could not find mapping for symbol 'some-symbol" ;hint: create the error message in a similar way you did for problem 14 of hw01.

Solutions

Expert Solution

Code: (using racket)

#lang racket
; List of keys
(define key '(I II III IV V))
; List of values
(define value '(1 '(2 7) 3 4))

; Function
; Name: create mapping
; Parameters: x (list of keys), y (list of values)
(define (create-mapping x y)
(lambda(s)   ; a function that displays the value according to the key symbol s
    (define index (member s x)) ; member function is used to check whether the element is present in the list
    (if (and index (> (length y) (- (length x) (length index)))) ; checking if symbol s is present in the key list x and they index of s
                                                                 ; is in the list of values.

    (list-ref y (- (length x) (length index))) ;list-ref returns the value from list y at the given position (- (length x) (length index)               

    (format "Error: Could not find mapping for symbol ~a" s))    ; if the symbol is not there in the keys or
                                                                 ; if the key does not have a value, error message is displayed
    )
)

; Testing the function.
; create-mapping returns a function and the function is named as mapped
(define mapped(create-mapping key value))

; Testing for keys
(mapped 'V)
(mapped 'IV)
(mapped 'P)

Output:

"Error: Could not find mapping for symbol V"
4
"Error: Could not find mapping for symbol P"

IDE:

Functions used:

1. length - returns the length of a given list
Syntax: (length list)

e.g.

(define k '(1 2 3))
(length k)

Output: 3

2. member - checks for the first instance of the given element in the list. If the element exists, it will return the tail of the list starting from the given element. If it is not present in the list, it will return #f.
Syntax: (member element list)

e.g.

(member 't '(r t y))
Output:
'(t y)

(member 'p '(t y))
Output:
#f
which is false in scheme language

3. list-ref - returns the element in list using the given position.If the position is greater than or equal to the length of the list, it will throw an error. The position starts from 0.
Syntax- (list-ref list position)

e.g.

(list-ref '(1 2 3) 2)
Output:
1


Related Solutions

Implement a method that does the following: (a) Create a Map data structure to hold the...
Implement a method that does the following: (a) Create a Map data structure to hold the associations between player name (key) and team affiliation (value) (b) Add at least 5 mappings to the map (c) Print out the current roster of mappings (d) Assuming some time has passed, change some of the mappings to simulate the players changing their affiliations (e) Again, print out the current roster of mappings 3. Implement a main method that calls the above methods, demonstrating...
Create a class Superheros member: Map heroMap //key = name, value = weapon (or get creative)...
Create a class Superheros member: Map heroMap //key = name, value = weapon (or get creative) member: Set< String> powerSet //superpowers *** initialize your map & set – preferably in constructor *** /* One liner to ‘put’ the key, value pair you’re passing in.. */ Method: void putEntryInMap (String key, String value) {} /* Another 1 liner – well maybe 2. (1) ‘get’ the value from the map with the key (2) capture that value and return it (to your...
In java. Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value)....
In java. Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value). addToMultiMap must add the value, if present, to the set associated with the given key, creating the set if necessary. You may assume all keys already in the map are associated with non-null values. For full credit, your method must include generic types, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness. You may assume Map,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT