sicp-solutions/ex-2.03.scm
Petar Kapriš 1b2b2dfec1 Add solutions to exercises from section 2.1
To be noted: the last two exercises, 2.15 and 2.16 were left unfinished,
and will be left for another time.
2025-02-05 15:17:47 +01:00

34 lines
791 B
Scheme

#lang sicp
(define (make-point x y)
(cons x y))
(define (x-point p)
(car p))
(define (y-point p)
(cdr p))
; top-left point, bottom-right point representation
(define (tlbr-rect a b)
(cons 'tlbr (cons a b)))
; center point, width, height representation
(define (cpwh-rect p w h)
(cons 'cpwh
(cons p
(cons w h))))
(define (height rect)
(cond ((eq? 'cpwh (car rect)) (cdddr rect))
(else (abs (- (y-point (cadr rect))
(y-point (cddr rect)))))))
(define (width rect)
(cond ((eq? 'cpwh (car rect)) (caddr rect))
(else (abs (- (x-point (cadr rect))
(x-point (cddr rect)))))))
(define (perimeter rect)
(* 2 (+ (width rect) (height rect))))
(define (area rect)
(* (width rect) (height rect)))