Created
February 3, 2016 07:13
-
-
Save sliminality/e4ae13ff139e6781d17c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-struct circle [radius color]) | |
; A Circle is a structure: (make-circle Number String) | |
(define circ1 (make-circle 4 "blue")) | |
(define circ2 (make-circle 2 "red")) | |
; ----------------------------------------------------- | |
(define-struct square [side color]) | |
; A Square is a structure: (make-square Number String) | |
; For purposes of this exercise, a square is not a rectangle | |
(define sq1 (make-square 4 "blue")) | |
(define sq2 (make-square 10 "green")) | |
; ----------------------------------------------------- | |
(define-struct rectangle [length width color]) | |
; A Rectangle is a structure: (make-rectangle Number Number String) | |
; where length and width are different | |
; because for purposes of this exercise, a rectangle is not a square | |
(define rect1 (make-rectangle 3 4 "blue")) | |
(define rect2 (make-rectangle 6 10 "red")) | |
; ----------------------------------------------------- | |
; Exercise 1a | |
; circle-is-blue? : Circle -> Boolean | |
; returns true if a Circle has color "blue," false otherwise | |
(check-expect (circle-is-blue? circ1) true) | |
(check-expect (circle-is-blue? circ2) false) | |
(define (circle-is-blue? c) | |
(eq? "blue" (circle-color c))) | |
; ----------------------------------------------------- | |
; A Shape is one of the following: | |
; - A Circle | |
; - A Square | |
; - A Rectangle | |
; ----------------------------------------------------- | |
; Exercise 1b | |
; shape-is-blue? : Shape -> Boolean | |
; returns true if a Shape has color "blue", false otherwise | |
(check-expect (shape-is-blue? circ1) true) | |
(check-expect (shape-is-blue? sq1) true) | |
(check-expect (shape-is-blue? rect2) false) | |
(define (shape-is-blue? s) | |
(cond | |
[(circle? s) (eq? "blue" (circle-color s))] | |
[(square? s) (eq? "blue" (square-color s))] | |
[(rectangle? s) (eq? "blue" (rectangle-color s))])) | |
; ----------------------------------------------------- | |
; A List-of-Shapes is one of the following: | |
; - empty | |
; - (cons Shape List-of-Shapes) | |
(define my-shape-list (list circ2 sq2 rect1 circ1 rect2 sq1)) | |
; generally speaking, we won't bother creating a nickname for an empty list | |
; since we can just test functions by typing the word 'empty' itself | |
; this is just to demonstrate that empty is a valid example of a List-of-Shapes | |
; according to our definition above | |
(define empty-shape-list empty) | |
; ----------------------------------------------------- | |
; Exercise 1c | |
; first-blue-shape? : List-of-Shapes -> Shape or Boolean | |
; returns the first Shape in the list with color "blue" | |
; if there are no Shapes with color "blue", return false | |
(check-expect (first-blue-shape? my-shape-list) rect1) | |
(check-expect (first-blue-shape? empty) false) | |
(define (first-blue-shape? los) | |
(cond | |
[(empty? los) false] | |
[(eq? "blue" | |
(cond | |
[(circle? (first los)) (circle-color (first los))] | |
[(square? (first los)) (square-color (first los))] | |
[(rectangle? (first los)) (rectangle-color (first los))])) | |
(first los)] | |
[else (first-blue-shape? (rest los))])) | |
; Exercise 1d | |
; first-blue-shape-helper : List-of-Shapes -> Shape or Boolean | |
; this time, use at least one helper function (it can do anything) | |
; Exercise 1e | |
; first-blue-shape-message : List-of-Shapes -> String | |
; rewrite first-blue-shape? but instead of returning the first matching Shape | |
; we return the string "Found a blue shape!" | |
; if there are no Shapes with color "blue", return "No blue shapes" | |
; now create a list of non-blue shapes | |
; and write tests for these | |
; I stopped numbering exercises because I'm lazy........ | |
; ----------------------------------------------------- | |
; square-area : Square -> Number | |
; return the area of the provided Square | |
; write your tests here | |
; write your function here | |
; rectangle-area : Rectangle -> Number | |
; return the area of the provided Rectangle | |
; write your tests here | |
; write your function here | |
; circle-area : Circle -> Number | |
; return the area of the provided Circle | |
; FYI, Racket recognizes pi as a number | |
; Another FYI, use pi in your check-expects to avoid annoying decimal mismatches | |
; write your tests here | |
; write your function here | |
; ----------------------------------------------------- | |
; shape-area : Shape -> Number | |
; return the area of the provided Shape | |
; USING THE THREE FUNCTIONS YOU WROTE IN THE PREVIOUS SECTION | |
; ----------------------------------------------------- | |
; sum-shape-area : List-of-Shapes -> Number | |
; return the sum of the areas of the Shapes in the provided List | |
; USE YOUR PREVIOUSLY WRITTEN HELPER FUNCTIONS | |
; NOW WRITE | |
; sum-shape-area-no-helpers : List-of-Shapes -> Number | |
; return the sum of the areas of the Shapes in the provided list | |
; YOU CAN'T USE ANY HELPER FUNCTIONS | |
; BUT YOU *CAN* COPY AND PASTE CODE FROM OLDER FUNCTIONS | |
; sorry i just started shouting for no reason | |
; ----------------------------------------------------- | |
; now define three more Lists-of-Shapes for our tests | |
; the first two lists should contain at least one blue shape | |
; the third list should not contain any blue shapes | |
; contains-blue-shape? : List-of-Shapes -> Boolean | |
; true if the List of Shapes contains a blue shape, false otherwise | |
; Hint: this will be very similar to an older function | |
; ----------------------------------------------------- | |
; blue-in-both? : List-of-Shapes List-of-Shapes -> Boolean | |
; given two Lists-of-Shapes, return true if both lists contain at least one blue shape | |
; ----------------------------------------------------- | |
; Super challenge question, whoo | |
; write the following program | |
; A Color is one of the following: | |
; - "red" | |
; - "blue" | |
; - "green" | |
; match-in-both? : List-of-Shapes List-of-Shapes Color -> Boolean | |
; given two Lists-of-Shapes and a Color, return true if both lists contain | |
; at least one shape matching the provided Color |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment