In: Computer Science
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.
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