;; 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 struct-intro-book-before) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #f #t none #f () #f))) ;; a book has info: ;; title, author, # of pages, is copyrighted? ;;; structs in racket, a "product type" ;;; i.e. aggregate data: take several pieces of data, ;;; and wrap them up together and consider it *one* thing ;;; (understanding it is comprised of several internal fields). ;;; "encapsulation" -- we put a capsule around the fields. ; Datatype Definiton: (define-struct book (title author num-pages is-copyrighted?)) ; A book is: ;(make-book [string] [string] [natnum] [boolean]) ; Examples of the data (define cih (make-book "The Cat in the Hat" "Dr. Seuss" (+ 2 35) #true)) (define b0 (make-book "The Soul of Wit" "Barland" 0 #false)) ; `define-struct` actually introduces additional functions: ; a constructor ; and four getters ("selectors"). ; In this case, the constructor is named ; make-book : string, string, natural, boolean -> book ; and the getters are named ; book-title : book -> string ; book-author ; book-num-pages : book -> natnum ; book-is-copyrighted? (book-title (make-book "The Cat in the Hat" "Seuss" 37 #true)) (define (foo a-book) (if (string=? "Cat" (book-title a-book)) ... ...)) ; reading-time : book -> non-negative real ; The amount of time it takes the average reader to read a book (in minutes) ; (define (reading-time a-book) (* (book-num-pages a-book) 2)) ; class Book{ int readingTime( /* Book this*/ ) { return 2*this.num-pages; } (check-expect (reading-time (make-book "The Cat in the Hat" "Seuss" 37 #true)) 74) (check-expect (reading-time b0) 0) ; book->string : book -> string ; Return a string-representation of a book struct, user-friendly. (define (book->string a-book) (string-append (book-title a-book) ", by " (book-author a-book) " (" (number->string (book-num-pages a-book)) "pp)" (if (book-is-copyrighted? a-book) ", ©" ""))) (check-expect (book->string (make-book "The Cat in the Hat" "Seuss" 37 #true)) "The Cat in the Hat, by Seuss (37pp), ©") (check-expect (book->string b0) "The Soul of Wit, by Barland (0pp)") (check-expect (enbiggen b0) b0) (check-expect (enbiggen (make-book "The Bat in the Mat" "Streuss" 1 #true)) (make-book "The Bat in the Mat" "Streuss" 2 #true)) (check-expect (enbiggen (make-book "The Cat in the Hat" "Seuss" 37 #true)) (make-book "The Cat in the Hat" "Seuss" 74 #true)) ; enbiggen : book -> book ; Return a book just like `a-book`, but with bigger text (hence, more pages). ; (define (enbiggen a-book) (make-book (book-title a-book) (book-author a-book) (* 2 (book-num-pages a-book)) (book-is-copyrighted? a-book))) ; derive-from ; Create a brand new book, based on an original. ; The result's author will be the original author & the `new-author`, ; and the result has `#new-pages` pages added to the original. ; ;;; template (we'll come back) ; func-for-book : book -> ?? ; (define (func-for-book a-book) (... (book-title a-book) (book-author a-book) (book-num-pages a-book) (book-is-copyrighted? a-book)))