Skip to main content Link Menu Expand (external link) Copy Copied

Lab 2

Partners

Work on this lab with someone who you haven’t worked with before. Your lab partner will change every week so that you can get to know your fellow computer scientists.

Finite Increasing Arithmetic Sequences

Below is the data definition of a finite increasing arithmetic sequence (FIAS).

;; A FIAS (Finite Increasing Arithmetic Sequence) is a:
;; (make-fias Number Number Positive)
(define-struct fias [min max step])
;; where (make-fias min max step) represents all numbers
;; of the form min + (k * step), where k is a natural number,
;; such that min + (k * step) < max.
 
(define fias-empty (make-fias 1 1 0.25)) ;; empty sequence, as min >= max
(define fias-1 (make-fias 0 1 0.25)) ;; sequence with the elements (0, .25, .5, .75)
 
;; fias-temp : FIAS -> ?
(define (fias-temp fias)
  (... (fias-min fias) ... (fias-max fias) ... (fias-step fias) ...))

TA Demo

  1. Design a function that determines if a FIAS is empty.

     ;; empty-fias? : FIAS -> Boolean
     ;; determines if the given FIAS is empty
     (check-expect (empty-fias? fias-empty) #t)
     (check-expect (empty-fias? fias-1) #f)
    
     (define (empty-fias? fias)
         (>= (fias-min fias) (fias-max fias)))
    
  2. Design a function that increases the min of a FIAS by its step.

     ;; rest-fias : FIAS -> FIAS
     ;; returns a new FIAS where the min is the original FIAS's min plus its step
     (define (rest-fias fias)
         (make-fias
             (+ (fias-min fias) (fias-step fias))
             (fias-max fias)
             (fias-step fias)))
    
  3. Revise your FIAS template to take advantage of the recursive structure induced by the previous to functions.

     ;; fias-temp : FIAS -> ?
     (define (fias-temp fias)
         (cond
             [(empty-fias? fias) ...]
             [else (... (fias-min fias) ... (fias-temp (rest-fias fias)) ...)]))
    

Activity

  1. Design a function that computes a list that is the element-wise sum of two FIASs, dropping the excess elements of the longer FIAS.
  2. Design a function that computes a list that is the element-wise product of two FIASs, dropping the excess elements of the longer FIAS.
  3. Abstract the previous two functions, redefine your functions to use the new abstraction,
    and ensure your tests still pass. Why can’t we output a FIAS instead of a list? Hint: consider the previous problem.
  4. Design a function that determines if any element of a FIAS is a perfect square.
  5. Design a function that determines if any element of a FIAS is even.
  6. Abstract the previous two functions, redefine your functions to use the new abstraction, and ensure your tests still pass.

Switch Pair Programming Roles

Abstracting Functions

  1. Design a function until that abstracts the following two functions. Replace these definitions with ones that use until, and ensure that the tests still pass.
     ;; until-negative : [List-of Number] -> [List-of Number]
     ;; computes the prefix of the list before the first negative number
     (check-expect (until-negative (list)) (list))
     (check-expect (until-negative (list 1 2 3)) (list 1 2 3))
     (check-expect (until-negative (list 1 2 -3 4 5)) (list 1 2))
    
     (define (until-negative lon)
         (cond
             [(empty? lon) '()]
             [(negative? (first lon)) '()]
             [else (cons (first lon) (until-negative (rest lon)))]))
    
     ;; until-empty : [List-of X] -> [List-of X]
     ;; computes the prefix of the list before the first empty string
     (check-expect (until-empty (list)) (list))
     (check-expect (until-empty (list "foo" "bar" "baz")) (list "foo" "bar" "baz"))
     (check-expect (until-empty (list "foo" "bar" "baz" "" "qux")) (list "foo" "bar" "baz"))
    
     (define (until-empty los)
         (cond
             [(empty? los) '()]
             [(string=? (first los) "") '()]
             [else (cons (first los) (until-empty (rest los)))]))
    
  2. Design a function are-all? that abstracts the following two functions. Replace these definitions with ones that use are-all?, and ensure that the tests still pass. Optional: try to simplify the definition of are-all?.
     ;; are-all-even? : [list-of number] -> boolean
     ;; are all of the numbers even?
     (check-expect (are-all-even? '()) #t)
     (check-expect (are-all-even? (list 1 10 100 -1)) #f)
     (check-expect (are-all-even? (list 1 15 -23)) #f)
     (check-expect (are-all-even? (list 10 100 -22)) #t)
    
     (define (are-all-even? lon)
         (cond
             [(empty? lon) #t]
             [else (and (even? (first lon))
                        (are-all-even? (rest lon)))]))
    
     ;; are-all-only-lowercase?: [list-of string] -> boolean
     ;; are all strings in the list only lowercase?
     (check-expect (are-all-only-lowercase? '()) #t)
     (check-expect (are-all-only-lowercase? (list "abc" "12a" "ui" "989")) #f)
     (check-expect (are-all-only-lowercase? (list "abc" "cde" "fgh")) #t)
    
     (define (are-all-only-lowercase? los)
         (cond
             [(empty? los) #t]
             [else (and (string-lower-case? (first los))
                        (are-all-only-lowercase? (rest los)))]))
    

    Switch Pair Programming Roles

List Abstractions

Consider the following data definition of Shape.

(define-struct circl [radius mode c])
(define-struct squar [side-length mode c])
(define-struct rectangl [width height mode c])

;; A Shape is one of:
;; - (make-circl Number Mode Color)
;; - (make-squar Number Mode Color)
;; - (make-rectangl Number Number Mode Color)

;; and represents either:
;; - the radius in pixels, mode, and color of a circle
;; - the side length in pixels, mode, and color of a square
;; - the width and height in pixels, the mode, and color of a rectangle

(define CIRC (make-circl 5 "solid" "green"))
(define SQUARE (make-squar 10 "outline" "blue"))
(define RECT (make-rectangl 15 20 "solid" "red"))

;; shape-template : Shape -> ?
(define (shape-template s)
  (cond
    [(circl? s)
     (... (circl-radius s) ... (mode-template (circl-mode s)) ... (circl-c s) ...)]
    [(squar? s)
     (... (squar-side-length s) ... (mode-template (squar-mode s)) ... (squar-c s) ...)]
    [(rectangl? s)
     (... (rectangl-width s) ... (rectangl-height s) ...
          (mode-template (rectangl-mode s)) ... (rectangl-c s) ...)]))

;; A Mode is one of:
;; - "solid"
;; - "outline"
;; and represents a the fill of an image

(define (mode-template m)
  (cond
    [(string=? m "solid") ...]
    [(string=? m "outline") ...]))
  1. Use ormap to design a function that takes a [List-of Shape] and determines if any of them are “green”.

  2. Use filter to design a function that takes a [List-of Shape] and keeps only circles.

  3. Use map to design a function that takes a [List-of Shape] and outputs their colors.

  4. Use foldr to design a function that takes a [List-of Shape] and stacks all of its images vertically. Hint: You may need to use map as well.

Signature Detective

Determine the signature of the following functions.

(define (a supercut of us)
  (+ of
     (if (empty? supercut)
         (us #f)
         (us (first supercut)))))

(define (come home to my heart)
  (cond [(home my) (to heart)]
        [(my heart) " "]
        [else ""]))