;; note: (define ex '((a b) (1 2)) = (list (list 'a 'b) (list 1 2)) (define ex `((a b) (1 2)) = (list (list 'a 'b) (list 1 2)) (define x `((a b) (1 2) (3 4) (5 6))) ;; Not the same as above (define x '((a b) (1 2))) (define x (list (list a b) (list 1 2) (list 3 4) (list 5 6))) ;; Question a. a. (map rest x) (list (rest (list a b)) (rest (list 1 2)) ...) (list (list b) (list 2) ...) b. (filter cons? x) x c. (andmap symbol? (map first x)) (map first x) -> (list 'a 1 3 5) (andmap symbol? (list 'a 1 3 5)) -> false ;; These are symbols: 'a '$ (ormap symbol? (map first x)) -> true d. (foldr + 0 (apply append (rest x))) ;; What does apply do? (apply + (list 1 2 3)) (+ 1 2 3) -> 6 (apply and (list true false true)) (and true false true) -> false (apply append (rest x)) -> (apply append (list (list 1 2) (list 3 4) (list 5 6)) -> (append (list 1 2) (list 3 4) (list 5 6)) -> (list 1 2 3 4 5 6) (foldr + 0 (list 1 2 3 4 5 6)) -> 21 a. (rest (list a b)) (rest (list 1 2)) d. (rest (list (list a b) (list 1 2) ...) ;; Problem 2 xs: [Listof X] g: [X-> Boolean] f: [X Y-> Z] par: Y Result: [Listof Z] [X -> Boolean] [X Y -> Z] Y [Listof X] -> [Listof Z] Order matters: If I wrote Y [X -> Boolean] [X Y -> Z] [Listof X] -> [Listof Z], then reader could think Y refers to the input g and this is WRONG ;; Problem 3 ;; andmap signature: [X -> Boolean] [List-of X] -> Boolean ;; foldr signature for andmap: [X Boolean -> Boolean] Boolean [List-of X] -> Boolean ;; andmap* : [X -> Boolean] [List-of X] -> Boolean ;; Find if all the elements in the given list satisfy the given test (andmap* symbol? (list 'a 1 3 5)) -> false (andmap* positive? (list 2 3 4)) -> true (define (andmap* func lox) (local [;; ander : [X Boolean -> Boolean] ;; Apply "and" to the given result of applying func to a x in ;; lox and to the given boolean ;; assuming func is "positive?" ;; given 5 and true, expect true ;; given -2 and true, expect false ;; given 0 and false, expect false ;; (ander 5 true) -> (and (func 5) true) (define (ander ax ab) (and ab (func ax)))] (foldr ander true lox))) ;; Problem 4 ;; sleepy-students : [List-of Student] -> [List-of Student] ;; Filters out the students that are awake (define student1 (make-student "Rachel" 'wed true '(80 100)) (define student2 (make-student "Eikas" 'mon false '(40 101)) (define lostudent1 empty) (define lostudent2 (list student1 student2)) (sleepy-students lostudent1) -> empty (sleepy-students lostudent2) -> (list student2) (define (sleepy-students a-los) (filter (lambda (a-student) (not (stundet-awake? a-student))) a-los)) ;; [X -> Boolean] [List-of X] -> [List-of X] ;; [List-of Student] -> [List-of String] ;; Returns the list of student names (define (list-names los) (map student-name los)) (check-expect ((lambda (x) (+ 5 x)) 1) 6) (check-expect (add-5 1) 6) ;; Problem 5 ;; [List-of Record] [List-of Exam] -> [List-of Record] ;; Adds the exam grade to each student's log (add-grade empty empty) -> empty (define (add-grade lor loe) (cond [(empty? lor) empty] [(cons? lor) (cons (update-record (first lor) (first loe)) (add-grade (rest lor) (rest loe)))])) ;; Record Exam -> Record ;; Returns an updated record with the given exam score (define (update-record rec ex) (make-record (record-name rec) (cons (record-log rec) (exam-grade ex) ))) ;; TESTS!!! :D (list 1 2 3) -> (list 6 7 8) (define (add-5-to-list lon) (map (lambda (x) (+ x 5)) lon) (checx-expect ((lambda (x) (+ x 5)) 1) 6)