Hi there!
I'm having trouble figuring out what would constitute a good stopping condition in this exercise where a function consumes a ListOfSchools and produces a School...
When such functions deal with numbers, I have no problem finding a good base case... But here?...
Or at least that's what I think the problem is...
Any hints on how to develop a better "mental image" of what a good stopping condition should be in general? Not just with numbers...
Thank you in advance for any help!
Here's my exercise bellow. My question is about the question (C), and the corresponding function I called 'lowest-tui'
(require 2htdp/image)
;; tuition-graph-starter.rkt (just the problem statements)
;
; PROBLEM:
;
; Eva is trying to decide where to go to university. One important factor for her is
; tuition costs. Eva is a visual thinker, and has taken Systematic Program Design,
; so she decides to design a program that will help her visualize the costs at
; different schools. She decides to start simply, knowing she can revise her design
; later.
;
; The information she has so far is the names of some schools as well as their
; international student tuition costs. She would like to be able to represent that
; information in bar charts like this one:
;
;
; .
;
; (A) Design data definitions to represent the information Eva has.
; (B) Design a function that consumes information about schools and their
; tuition and produces a bar chart.
; (C) Design a function that consumes information about schools and produces
; the school with the lowest international student tuition.
;
; DOMAIN ANALYSIS:
; ================
;
;
; CONSTANT INFORMATION:
; =====================
; - Bar color
; - Bar width
; - text font size
; - text color
; - Y-scale
;
; CHANGING INFORMATION:
; =====================
; -Number of bars
; -Bar height
; -School names
; -Number of schools
;
; ===
; !NO BIG-BANG OPTION!
;; CONSTANTS:
;;===========
(define BAR-COLOR "lightblue")
(define BAR-WIDTH 50)
(define FONT-SIZE 25)
(define FONT-COLOR "black")
(define Y-SCALE 1/30)
;; DATA DEFINITIONS:
;;==================
;; (@htdd school)
(define-struct school (name tuition))
;; School is (make-school String Natural)
;; Interp. A school with:
;; - name as the school's name
;; - tuition as student's tuition in USD
(define S1 (make-school "Lemsid" 5800)) ; School named Lemsid, with a tuition of 5800 USD
(define S2 (make-school "Ellissi" 4500)) ; School named Ellissi, with a tuition of 4500 USD
(define S3 (make-school "Abd ElMoumen" 7800)) ; School named Abd ElMoumen with a tuition of 7800 USD
#;
(define (fn-for-school s)
(... (school-name s) ; String
(school-tuition s))) ; Natural
;; Templates rules used:
;; - compound: 2 fields
;;===
;; (@htdd ListOfSchool)
;; ListOfSchool is one of:
;; - empty
;; - (cons School ListOfSchool)
;; Interp. A list of schools
(define LOS1 empty)
(define LOS2 S1)
(define LOS3 (cons S1
(cons S2
(cons S3
empty))))
#;
(define (fn-for-los los)
(cond [(empty? los) (...)]
[else
(... (fn-for-school (first los))
(fn-for-los (rest los)))]))
;; Template rules used:
;; - one of 2 cases
;; - atomic distict: empty
;; - compound: 2 fields
;; - reference (first los) is School
;; - self-reference: (rest los) is ListOfSchool
;;===
;; FUNCTIONS:
;;===========
;; (@htdf barchart)
;; (@Signature: ListOfSchool -> Image)
;; Produces a bar chart of a given list of schools and their respective tuitions.
(check-expect (barchart empty)
(square 0 "solid" "white"))
(check-expect (barchart (cons S1
(cons S2 empty)))
(beside/align "bottom"
(overlay/align "center" "bottom"
(rectangle BAR-WIDTH
(* 5800 Y-SCALE) "outline" "pink")
(rotate 90 (text "Lemsid" FONT-SIZE FONT-COLOR))
(rectangle BAR-WIDTH
(* 5800 Y-SCALE) "solid" BAR-COLOR))
(overlay/align "center" "bottom"
(rectangle BAR-WIDTH
(* 4500 Y-SCALE) "outline" "pink")
(rotate 90 (text "Ellissi" FONT-SIZE FONT-COLOR))
(rectangle BAR-WIDTH
(* 4500 Y-SCALE) "solid" BAR-COLOR))))
;; (define (barchart los) (square 0 "solid" "white")) ;stub
(define (barchart los)
(cond [(empty? los) (square 0 "solid" "white")]
[else
(beside/align "bottom"
(draw-bar (first los))
(barchart (rest los)))]))
;;===
;; (@htdf draw-bar)
;; (@Signature: School -> image)
;; Produces an image of a bar for an single school,
;; with a bar height that depends on the cost of the school's tuition
(check-expect (draw-bar S1)
(overlay/align "center" "bottom"
(rectangle BAR-WIDTH
(* 5800 Y-SCALE) "outline" "pink")
(rotate 90 (text "Lemsid" FONT-SIZE FONT-COLOR))
(rectangle BAR-WIDTH
(* 5800 Y-SCALE) "solid" BAR-COLOR)))
;; (define (draw-bar s) (square 0 "solid' 'red")) ; stub
(define (draw-bar s)
(overlay/align "center" "bottom"
(rectangle BAR-WIDTH
(* (school-tuition s) Y-SCALE) "outline" "pink")
(rotate 90 (text (school-name s) FONT-SIZE FONT-COLOR))
(rectangle BAR-WIDTH
(* (school-tuition s) Y-SCALE) "solid" BAR-COLOR)))
;;===
;; (@hdtf lowest-tui)
;; (@Signature ListOfSchool -> School)
;; Produces the school with the lowest tuitition in a given list
(check-expect (lowest-tui empty)
(make-school "" 0))
(check-expect (lowest-tui LOS3)
S2)
;; (define (lowest-tui s) S1) ; stub
(define (lowest-tui los)
(cond [(empty? los) (make-school "" 0)]
[else
(if (< (school-tuition (first los)) (school-tuition (lowest-tui (rest los))))
(first los)
(lowest-tui (rest los)))]))
;; (barchart LOS3)