;; The first three lines of this file were inserted by DrRacket. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname examreview) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ()))) (define x '((a b) (1 2) (3 4) (5 6))) (check-expect '(()) (list empty)) (check-expect (length x) 4) (check-expect (first x) (list 'a 'b)) (check-expect (rest x) '((1 2) (3 4) (5 6))) (check-expect (map rest x) (list (list 'b) (list 2) (list 4) (list 6))) (check-expect (filter cons? x) x) (check-expect (andmap symbol? (map first x)) false) (check-expect (ormap symbol? (map first x)) true) ;; apply : [X Y Z... -> A] x (list X Y Z...) -> A (check-expect (apply + '(1 2 3)) 6) (check-expect (apply > '(3 2 1)) true) ;; apply : [A B C D E... -> Z] x A x B x C ... ;; (list D E...) -> Z ;; Last element must be a list - ;; this is the more confusing, ;; less used version (check-expect (apply + 1 2 '(3 4 5 6)) 21) (check-expect (apply foldr (list string-append "" '("hello " "world"))) "hello world") ;; apply is confusing - learn other loops ;; first (check-expect (foldr + 0 (apply append (rest x))) (apply + '(1 2 3 4 5 6))) ;; general-contract-example: ;; map : [X -> Y] [Listof X] -> [Listof Y] ;; foo : [X -> Boolean] [X Z -> Y] Z [Listof X] -> [Listof Y] (define (foo g f par xs) (cond [(empty? xs) xs] [(cons? xs) (if (g (first xs)) (cons (f (first xs) par) (foo g f par (rest xs))) (foo g f par (rest xs)))])) ;; myandmap : [X -> Boolean] [List X] -> Boolean ;; Do all items in the list past the test? (define (myandmap f lox) ;; foldr : [X Y -> Y] Y [List X] -> Y ;; foldr : [X Boolean -> Boolean] Boolean [List X] -> Boolean (foldl (λ (x bool) (and (f x) bool)) true lox)) (check-expect (myandmap number? empty) (andmap number? empty)) (check-expect (myandmap number? empty) true) (check-expect (myandmap number? '(1 2 3 4)) (andmap number? '(1 2 3 4))) (check-expect (myandmap number? '(1 2 3 4)) true) (check-expect (myandmap number? '(1 2 a 3 4)) (andmap number? '(1 2 a 3 4))) (check-expect (myandmap number? '(1 2 a 3 4)) false) ;; mylocalandmap : [X -> Boolean] [List X] -> Boolean ;; Do all items in the list past the test? (define (mylocalandmap f lox) ;; foldr : [X Y -> Y] Y [List X] -> Y ;; foldr : [X Boolean -> Boolean] Boolean [List X] -> Boolean (local [;; check : X Boolean -> Boolean ;; Check if x passes the test ;; and the rest of the list also does (define (check x bool) (and (f x) bool))] (foldr check true lox))) (define-struct student (name lab awake? quizzes)) ;; A Student is a (make-student String Symbol ;; Boolean [Listof Number]) ;; where: lab - one of ‘mon or ‘wed ;; awake? - true if the student asks ;; and answers questions in class ;; quizzes - the list of grades assigned ;; for the class quizzes (define STUDENT1 (make-student "Sarah" 'wed false '(0 0 1 0))) (define STUDENT2 (make-student "Blake" 'wed true '(0 0 0 0))) (define LIST1 empty) (define LIST2 (list STUDENT2)) (define LIST3 (cons STUDENT1 LIST2)) ;; sleepy-students : [Listof Student] -> [Listof Student] ;; return students that are not awake (define (sleepy-students los) (filter (λ (s) (not (student-awake? s))) los)) (check-expect (sleepy-students LIST1) empty) (check-expect (sleepy-students LIST2) empty) (check-expect (sleepy-students LIST3) (list STUDENT1)) ;; student-names : [Listof Student] -> [Listof String] ;; return the names of all the students in the list (define (student-names los) (map (λ(s) (student-name s)) los)) (check-expect (student-names LIST1) empty) (check-expect (student-names LIST3) (list "Sarah" "Blake")) ;; a Record is a ;; (make-record String [Listof Number]) (define-struct record (name log)) ;; where log is a [Listof Number] representing a ;; list of exam grades (define r1 (make-record "Mary" '(80 90))) (define r2 (make-record "Bob" '(82 85))) ;; an Exam is a (make-exam String Number) (define-struct exam (name grade)) (define e1 (make-exam "Mary" 92)) (define e2 (make-exam "Bob" 89)) ;; add-grade : [Listof Record] [Listof Exam] -> [Listof Record] ;; adds the grades in the list of exams to each student's ;; list of grades (define (add-grade lor loe) (map (λ (r e) (make-record (record-name r) (cons (exam-grade e) (record-log r)))) lor loe)) (check-expect (add-grade (list r1 r2) (list e1 e2)) (list (make-record "Mary" '(92 80 90)) (make-record "Bob" '(89 82 85)))) ;; loops to know: map, foldr, foldl, filter, ormap, andmap, ;; sort, build-list, apply ;; ! : Natural -> Positive ;; x! (define (! x) (apply * (build-list x add1))) #;(cond [(zero? x) 1] [else (* x (! (sub1 x)))]) (check-expect (! 0) 1) (check-expect (! 4) (* 4 3 2 1)) (check-expect (build-list 5 sqr) '(0 1 4 9 16)) ;; sort-strings : [Listof String] -> [Listof String] ;; sorts a list of strings in descending order (define (sort-strings los) ;; sort : [Listof X] [X X -> Boolean] -> [Listof X] ;; sort : [Listof String] [String String -> Boolean] -> [Listof String] (sort los string-ci PowerSet ;; Output the powerset of a set (define (powerset set) ;; foldr : [X Y -> Y] Y [List X] -> Y ;; foldr : [X PowerSet -> PowerSet] PowerSet Set -> PowerSet (foldr (λ (element ps) (append (map (λ (subset) (cons element subset)) ps) ps)) (list empty) set)) (check-expect (powerset empty) (list empty)) (check-expect (powerset '(vanilla chocolate strawberry)) (list (list 'vanilla 'chocolate 'strawberry) (list 'vanilla 'chocolate) (list 'vanilla 'strawberry) (list 'vanilla) (list 'chocolate 'strawberry) (list 'chocolate) (list 'strawberry) empty)) ;; fun with foldr (define (myreverse lox) (foldl cons empty lox)) (define (myidentity lox) (foldr cons empty lox)) (define (myappend l1 l2) (foldr cons l2 l1)) ;; good luck everyone!! :)) ;; stuff we didn't do #| ;;Assume l1 and l2 is not empty (define (foo l1 l2) (local((define (heyyo ...) ....) (define (whatever l1 l2) (cond[(and (empty? l1) (empty? l1)) 0] [(empty? l2) (rest l1)] [(empty? l1) (rest l2)] [else (heyyo (rest l3) (rest l4))]))) (append (rest l1) (rest l2)))) (define (whatever l3 l4) (cond[(and (empty? l3) (empty? l4)) 0] [(empty? l3) (rest l4)] [(empty? l4) (rest l5)] [else (heyyo (rest l3) (rest l4))])) (define (heyyo l3 l4) (cond [(or (empty? l3) (empty? l4)) empty] [else (append (rest l3) (rest l4))])) |#