;; 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-beginner-reader.ss" "lang")((modname 2015-10-05-lerner) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp")) #f))) ;; A Backpack is a (make-backpack Color Number) ;; color is the color of the backpack ;; weight is the maximum weight the backpack can hold in pounds (define-struct backpack (color weight)) ;; backpack-temp : Backpack -> ? (define (backpack-temp backpack) (... (backpack-color backpack) ... (backpack-weight backpack) ...)) ;; EXAMPLES (define backpack1 (make-backpack "black" 14)) ;; paint-backpack : Backpack Color -> Backpack ;; Paints backpack with the new color (define (paint-backpack backpack new-color) (make-backpack new-color (backpack-weight backpack))) (check-expect (paint-backpack backpack1 "red") (make-backpack "red" 14)) ;; A Student is a (make-student String Number Backpack) ;; represents a student's name, age, and backpack (define-struct student (name age backpack)) ;; EXAMPLES (define student1 (make-student "Chase" 18 backpack1)) ;; student-temp : Student -> ? (define (student-temp student) (... (student-name student) ... (student-age student) ... (backpack-temp (student-backpack student)) ...)) ;; paint-student-backpack : Student Color -> Student ;; Paint student's backpack to the new color (define (paint-student-backpack student color) (make-student (student-name student) (student-age student) (paint-backpack (student-backpack student) color))) (check-expect (paint-student-backpack student1 "pink") (make-student "Chase" 18 (make-backpack "pink" 14))) ;; A Book is a (make-book String String Number) ;; the title, author, and weight in pound of the book (define-struct book (title author weight)) ;; Examples, template... ;; A SOB (StackOfBooks) is one of: ;; - 'no-book ;; - (make-sob Book SOB) (define-struct sob (book stack)) ;; sob-temp : SOB -> ? (define (sob-temp sob) (cond [(symbol? sob) ...] [else (... (sob-book sob) ... (sob-temp (sob-stack sob)) ...)])) ;; EXAMPLES (define sob1 'no-book) (define sob2 (make-sob (make-book "HTDP" "Matthias" 1000) sob1)) (define sob3 (make-sob (make-book "Bible" "God" 500) sob2)) (define sob4 (make-sob (make-book "HGttG" "Adams" 10) sob1)) ;; can-carry? : Student SOB -> Boolean ;; Determines if student can carry sob (define (can-carry? student sob) (<= (sob-weights sob) (backpack-weight (student-backpack student)))) (check-expect (can-carry? student1 sob1) true) (check-expect (can-carry? student1 sob2) false) (check-expect (can-carry? student1 sob3) false) (check-expect (can-carry? student1 sob4) true) ;; sob-weights : SOB -> Number ;; Computes the total weight of sob (define (sob-weights sob) (cond [(symbol? sob) 0] [else (+ (book-weight (sob-book sob)) (sob-weights (sob-stack sob)))])) (check-expect (sob-weights sob1) 0) (check-expect (sob-weights sob2) 1000) (check-expect (sob-weights sob3) 1500)