Created
May 26, 2014 18:44
-
-
Save apeace/373fa67ab5751820a5d5 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
#lang racket | |
(require 2htdp/image) | |
(require test-engine/racket-tests) | |
(struct person (name year-born fav-color)) | |
(define CURRENT-YEAR 2014) | |
(define andrew (person "Andrew" 1989 (color 20 90 200 255))) | |
(define derp (person "Derp" 1901 (color 232 121 204 255))) | |
(define baby (person "Baby" 2013 'green)) | |
; person -> number | |
; the age (in years) of the given person | |
(define (person-age p) | |
(- CURRENT-YEAR (person-year-born p))) | |
; tests for our person-age function | |
(check-expect (person-age baby) 1) | |
(check-expect (person-age andrew) 25) | |
; person -> string | |
; a friendly greeting for the given person | |
(define (say-hi p) | |
(let* ([age (person-age p)] | |
[year-label (if (> age 1) "years" "year")] | |
[age-str (number->string age)]) | |
(string-append "Hi, " | |
(person-name p) | |
"! You are " | |
age-str | |
" " | |
year-label | |
" old."))) | |
; tests for our say-hi function | |
(check-expect (say-hi baby) "Hi, Baby! You are 1 year old.") | |
(check-expect (say-hi andrew) "Hi, Andrew! You are 25 years old.") | |
(define (hi-message p) | |
(text/font (say-hi p) 24 (person-fav-color p) "Gill Sans" 'swiss 'normal 'bold #f)) | |
(hi-message andrew) | |
(hi-message derp) | |
(hi-message baby) | |
(define (trick-person p) | |
(person (string-append (person-name p) "butt") | |
(person-year-born p) | |
(person-fav-color p))) | |
;(define people (list andrew derp baby)) | |
;(map hi-message (map trick-person people)) | |
(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment