Patfa01
Patfa01

Reputation: 1

How can I make this code functional for rectangles? (Scheme / Typed racket)

this is the code I've written, the functionis meant to: Consume a list of images (aloim) and a positive number n. It produces ; the first image in aloim that is not an n by n square; if it cannot ; find such an image, it produces #false I've gotten it to work for just squares, but if I add a rectangle to the list of images, it doesnt work, and I cant figure out how to fix it. would be awesome if someone could point me in the right direction!

#lang typed/racket
(require typed/test-engine/racket-tests)
(require typed/2htdp/image)
(require typed/2htdp/universe)

(define-type ImageOrFalse (U Image Boolean))

(: ill-sized : (Listof Image) Integer -> ImageOrFalse)
; Consumes a list of images aloim and a positive number n. It produces
; the first image in aloim that is not an n by n square; if it cannot
; find such an image, it produces #false

(define A (square 10 "solid" "blue"))
(define B (square 30 "solid" "red"))
(define C (square 15 "solid" "black"))
(define D (square 20 "solid" "black"))

(define ListA (cons A (cons B '())))
(define ListB (cons B (cons C '())))
(define ListC (cons A '()))
(define ListD (cons A (cons B (cons C (cons D '())))))
(define ListE '())

(define (ill-sized aloim n)
  (cond [(empty? aloim) #false]
        [(cons? aloim)
         (if (and
              (not (eq? (image-width (first aloim)) n))
              (not (eq? (image-height (first aloim)) n)))
             (first aloim)
                  (ill-sized (rest aloim) n))]))
     

(check-expect (ill-sized ListA 10) B)
(check-expect (ill-sized ListB 10) B)
(check-expect (ill-sized ListC 10) #false)
(check-expect (ill-sized ListD 20) A)
(check-expect (ill-sized ListE 30) #false)

          
(test)

Upvotes: 0

Views: 86

Answers (1)

tjorchrt
tjorchrt

Reputation: 702

I think you input wrong check-expect output. I use #lang racket in here I think no any different.

If you only want check square just use filter remove all rectangle than send list to ill-sized.

#lang racket
(require test-engine/racket-tests)
(require 2htdp/image)
(require 2htdp/universe)

(define (ill-sized aloim n)
  (cond
    [(empty? aloim) #false]
    [(or (= n (image-width (first aloim)))
         (= n (image-height (first aloim))))
     (first aloim)]
    [else
     (ill-sized (rest aloim) n)]))
     
(define (square-side=n aloim n)
  (local [(define (square? img)
            (= n (image-width img) (image-height img)))]
    (ill-sized (filter square? aloim) n)))

;;; TEST

(define A (square 10 "solid" "blue"))
(define B (square 30 "solid" "red"))
(define C (square 15 "solid" "black"))
(define D (square 20 "solid" "black"))
(define E (rectangle 40 20 "outline" "black"))
(define F (rectangle 20 50 "solid" "blue"))
(define list-of-images (list A B E F C D))

(check-expect (ill-sized list-of-images 60) #false)
(check-expect (ill-sized list-of-images 20) E)
(check-expect (ill-sized list-of-images 50) F)
(check-expect (square-side=n list-of-images 20) D)
(test)

Upvotes: 0

Related Questions