subtype structure

07 Mar 2012

(define-struct posn (x y))
(define-struct shape (color))
(define-struct (circle shape) (center radius))
(define-struct (square shape) (nw width))

(define a-shape (make-shape 'red))
(define a-circle (make-circle 'white (make-posn 100 100) 50))
(define a-square (make-square 'black (make-posn 100 100) 100))

(shape? a-shape) ; #t
(shape? a-circle) ; #f
(circle? a-shape) ; #f
(circle? a-circle) ; #f

(shape-color a-circle) ; 'white
(circle-color a-circle) ; !error
Based on Bootstrap. Icons from Font Awesome. Web fonts from Google. Generated by Jekyll.