Structures

The “proper” way of making structures is to use the struct special form. Always use the #:transparent option. The “constructor” is the same name as the structure. (In #lang racket, the default is not to use the make-* form.)

(struct three-d (x y z) #:transparent)
(define demo (three-d 5 12 13))

Accessing the fields of a structure is done by the function structname-fieldname. For example, (three-d-y demo) is 12.

Ordered pairs are called posn and can be used by (require lang/posn). They

  1. Give two undesirable consequences of not using #:transparent.

     (struct bad (p q))
     (define b1 (bad 5 12))
     (define b2 (bad 5 12))
     b1
     b2
     (equal? b1 b2)
    
  2. Create a structure called line which holds two points, p1 and p2.

  3. Write a function line-slope that takes in a line and computes the slope of the line. Include a check-expect. Instead of generating an error for vertical lines, create an specific error message explaining that the slope of a vertical line is undefined.

  4. (Cramer’s Rule) Use (list a1 b1 c1) to represent the equation a1*x+b1*y=c1.

    1. Write a determinant function that works for 2x2 matrices by just taking in four inputs a, b, c, and d.
    2. Write a Cramer’s Rule function that takes in two lists that represent the two equations in the system to solve.