In Struct.html we saw examples of structs for a person and Turing machine instructions and configurations.
Below we give you a practice problem for structs: a Cartesian coordinate point.
(struct point (x y) #:transparent)
(define p1 (point 0 0))
(define p2 (point 3 4))
p1
(point 0 0)
p2
(point 3 4)
A point has both an x and y coordinate. Above, we define p1
to be the origin and p2
to be a point a (3,4).
Your first job is to write a procedure good-point?
which checks to see if a given object is legimate point, including numeric coordinates.
(good-point? p1)
#t
(good-point? p2)
#t
(good-point? (point 'x 'y))
#f
(good-point? '(0 0))
#f
(good-point? (point 0+i 0-i))
#t
Note that complex numbers are still numbers.
Next, write a procedure line
which takes two points as arguments and returns the length of the line connecting those two points. It complains if you give it bogus points.
(line p1 p2)
5
(line p1 (point -3 -4))
5
(line p1 (point 0+3i 0+4i))
0+5i
(line p1 '(0 0))
'bad-points
(line p1 (point 'x 'y))
'bad-points
For the solution, see point.rkt