;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Lab3 -- Solutions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; PROBLEM 2: Problems on lists-of-numbers ;; ;; minimum: list-of-numbers -> number ;; find minimum value on alon ;; (define (minimum alon) ... ) ;;;;;;; Examples ; "(minimum empty) answer: empty" ; "(minimum '(-2)) answer: -2" ; "(minimum '(27 11 -2 5 -4)) answer: -4)" ;;;;;;;;Template for functions on list-of-numbers ;(define (fun-on-a-lons a-lons) ; (cond ; [(empty? a-lons) ...] ; [else ; (first a-lons) ... ; (fun-on-a-lons (rest a-lons)) ...])) ;;;;;;; Body ;; Need to consider (empty? rest a-lons) ;; Consider what happens to (minimum (cons -2 empty)) (define (minimum a-lons) (cond [(empty? a-lons) 'empty] [(empty? (rest a-lons)) (first lons)] [(< (first a-lons) (minimum (rest a-lons))) (first a-lons )] )) ;;;;;;; Tests "(minimum empty) answer: empty" (minimum empty) "(minimum '(-2)) answer: -2" (minimum '(-2)) "(minimum '(27 11 -2 5 -4)) answer: -4)" (minimum '(27 11 -2 5 -4)) ;; remove-nums: list-of-numbers number -> list-of-numbers ;; remove all numbers less than a-num from a-lons ;; (define (remove-nums alon a-num) ... ) ;;;;;;; Examples ; "(remove-nums empty 5) answer: empty" ; "(remove-nums '(2 8) 5) answer: '(8)" ; "(remove-nums '(7 12) 5) answer: '(7 12)" ; "(remove-nums '(5) 5) answer: '(5)" ;;;;;;;;Template ;;;;;;;; Template for function with extra parameters besides a-lons ;(define (fun2-on-a-lons a-lons a-num) ; (cond ; [(empty? a-lons) ...] ; [else ; (first a-lons) ... ; (fun-on-a-lons (rest a-lons) a-num) ...])) ;;;;;;; Body (define (remove-nums a-lons a-num) (cond [(empty? a-lons) a-lons] [(< (first a-lons) a-num) (remove-nums (rest a-lons) a-num)] [ else ;;; (first a-lons) belongs on list (cons (first a-lons) (remove-nums (rest a-lons) a-num)) ])) ;;;;;;; Tests "(remove-nums empty) answer: empty" (remove-nums empty 5) "(remove-nums '(2 8) 5) answer: '(8)" (remove-nums '(2 8) 5) "(remove-nums '(7 12) 5) answer: '(7 12)" (remove-nums '(7 12) 5) "(remove-nums '(5) 5) answer: '(5)" (remove-nums '(5) 5) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; PROBLEM 3: Help Your Grader ;; ;; a student is a ;; (make-student symbol symbol) (define-struct student (name grade)) ; constructor: make-student ; predicate: student? ; selectors: student-name student-grade ;;;;;;; Basic Templates for functions on Students: ;;;;; Template for Function on Student ;;;; A student is a compound data object ; (define (fun-for-stud stud) ; ... (student-name stud) ... ; ... (student-grade stud) ... ) ;;;; Template for Function on lists-of-students ;;;; Template calls fun-for-stud ; (define (fun-for-alost a-lost) ; (cond ; [(empty? alost) ...] ; [else ; ... (fun-for-stud (first a-lost)) ... ; ... (fun-for-alost (rest a-lost)) ...])) ;; get-grade: list-of-students symbol -> symbol ;; given alost and name (of student) return grade if student on list ;; Return 'None if student not on list ;; (define (get-grade alost name) ...) ;;;;;;;;Examples ;; "(get-grade empty 'ken), answer: 'None" ;; "(get-grade (list (make-student 'ken 'A) ; (make-student 'robby 'B) ; (make-student 'jacob 'C)) ; 'ken), answer: 'A" ;; "(get-grade (list (make-student 'ken 'A) ; (make-student 'robby 'B) ; (make-student 'jacob 'C)) ; 'kate), answer: 'None" ;;;;;;;; Body ;;;; Template for Function on lists-of-students ;;;; with one parameter ;;;; See Template fun-for-stud ; (define (fun-for-alost2 a-lost a-name) ; (cond ; [(empty? alost) ...] ; [else ; ... (fun-for-stud (first a-lost)) ... ; ... (fun-for-alost2 (rest a-lost) a-name) ...])) (define (get-grade alost name) (cond [(empty? alost) 'none] [(symbol=? name (student-name (first alost))) (student-grade (first alost))] [else (get-grade (rest alost) name)])) ;;;;;;;; Tests "(get-grade empty 'ken), answer: 'None" (get-grade empty 'ken) "(get-grade (list (make-student 'ken 'A) (make-student 'robby 'B) (make-student 'jacob 'C)) 'ken), answer: 'A" (get-grade (list (make-student 'ken 'A) (make-student 'robby 'B) (make-student 'jacob 'C)) 'ken) "(get-grade (list (make-student 'ken 'A) (make-student 'robby 'B) (make-student 'jacob 'C)) 'kate), answer: 'None" (get-grade (list (make-student 'ken 'A) (make-student 'robby 'B) (make-student 'jacob 'C)) 'kate) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; I leave the following for you to practice. ;; ;; get-names: list-of-students symbol -> list-of-symbols ;; Return list of all names of students with given grade ;; (define (get-names alost grade)...) ;; update-grade: list-of-students name grade -> list-of-students ;; update alost by changing student-grade to grade for name ;; (define (update-student alost name grade) ... ) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; PROBLEM 4: Binary-to-Decimal ;; ;; bin->dec: list-of-bits -> number ;; convert alob to decimal value, assuming alob starts from least ;; significant bit to most significant bit ;; (define (bin->decs alob) ... ) ;;;;;;;;;; Examples ;; "(bin->dec empty), answer: 0 ;; "(bin->dec '(1)), answer: 1 ;; "(bin->dec '(0 1)), answer: 2 ;; "(bin->dec '(1 0 1 1)), answer: 13 ;;;;;;;;Template for functions on list-of-numbers ;(define (fun-on-a-lons a-lons) ; (cond ; [(empty? a-lons) ...] ; [else ; (first a-lons) ... ; (fun-on-a-lons (rest a-lons)) ...])) (define (bin->dec a-lons) (cond [(empty? a-lons) 0] [else (+ (first a-lons) (* 2 (bin->dec (rest a-lons))))])) ;;;;;;;;;; Tests "(bin->dec empty), answer: 0" (bin->dec empty) "(bin->dec '(1)), answer: 1" (bin->dec '(1)) "(bin->dec '(0 1)), answer: 2" (bin->dec '(0 1)) "(bin->dec '(1 0 1 1)), answer: 13" (bin->dec '(1 0 1 1)) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;