Typed Exercises 2
-
ezn
: Create a list ofn
numbers, counting from n down to 0.(check-expect (ezn 5) (list 5 4 3 2 1 0)) (check-expect (ezn 7) (list 7 6 5 4 3 2 1 0))
-
rpt
: Given a word and a number, make a list containing that word the given number of times, except chop off one more letter at the start of the word each time you repeat it.(check-expect (rpt "word" 4) (list "word" "ord" "rd" "d")) (check-expect (rpt "cat" 2) (list "cat" "at"))
-
perfect-squares
: Given a list of integers, return a list of all of the perfect squares contained in that list. If you work with real numbers, you probably wantNonnegative-Real
numbers. -
grow-sq
: Given a start and end integer, make a list of images, all outline squares beginning at side length start, and ending at side length <= end. -
sqc
: Given two integersstart
andend
, draw one image which is an overlay of images as n goes fromstart
toend
increasing by 1 each time. The image is an outline circle of radius10*n
when n is even and an outline square of side length20*n
when n is odd.
Facts about Numbers
There are many number types, including:
Number
Real
Integer
Nonnegative-Real
Nonnegative-Integer
Positive-Integer
Pick the most appropriate one. For example, if you are taking the
square root of a number, Nonnegative-Real
might be a good choice. If
that number is also an integer, then use Nonnegative-Integer
.
The type Number
actually means complex number
(like 5+12i
from math class), so lots of the time you really want to
use Real
.
Trouble spots
When you subtract one from a positive integer, Racket no longer knows it is positive (could have turned into zero). However, adding one to a positive integer gives you something that is definitely positive. That means when you are working with positive numbers and you have a choice between counting up or counting down, count up.
Pro tip: if you have an Integer
and the error message tells you that
a Positive-Real
is needed, you should change your type to
Positive-Integer
.