SCHEME LANGUAGE QUESTION A map object is a list of length 5 with the following f
ID: 3855444 • Letter: S
Question
SCHEME LANGUAGE QUESTION
A map object is a list of length 5 with the following fields: a type (symbol), a label (string), coordinates (pair), direction (integer) and speed (integer). Given a list of map objects, write the following Scheme procedures: (40 points)
a. (get-type map-object) returns the type of map-object
b. (get-label map-object) returns the label of map-object
c. (get-loc map-object) returns the location of map-object
d. (collision? obj1 obj2) returns the true if obj1 & obj2 are in the same location, false otherwise
e. (get-all-of-type database type) returns a list of all objects in database of type type.
f. (find-object database label) returns the map object in database with label label.
(define map-database
'( (CAR "RedCar" (10 . 12) 0 65)
(POLICE "Police1" (18 . 10) 180 55)
(TREE "Tree1" (22 . 2) 180 0)
(OBSTACLE "Mattress1" (20 . 12) 180 0)))
> (find-object map-database "RedCar")
(CAR "RedCar" (10 . 12) 0 65)
Explanation / Answer
(define (get-type map-object ) (car map-object) )
(define (get-label map-object) (car (cdr map-object)) ) )
(define (get-loc map-object ) (car (cdr (cdr map-object) ) ) )
(define (collision? obj1 obj2) (if (and (equal? (car (get-loc obj1 ) ) (car (get-loc obj2 ) ) ) (equal? (cdr (get-loc obj1) ) (cdr (get-loc obj2) ) ) ) #t #f) )
(define (get-all-of-type map-database type) (if (null? map-database) '() (if (equal? (get-type (car map-database)) type) (cons (car map-database) (get-all-of-type (cdr map-database) type) ) (get-all-of-type (cdr map-database) type) ) ) )
(define (find-object map-database label) (if (null? map-database) '() (if (equal? label (get-label (car map-database))) (car map-database) (find-object (cdr map-database) label) ) ) ) )
;call functions below
;example
(get-all-of-type '((CAR "cf" (1.2) 185 25) (POLICE "fd" (5.5) 45 4) (CAR "=%" (2.3) 45 56)) 'CAR) ) )
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.