Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Created November 2, 2019 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/39a41d0ed44a147b96d0e9bfdb728d4a to your computer and use it in GitHub Desktop.
Save chelseatroy/39a41d0ed44a147b96d0e9bfdb728d4a to your computer and use it in GitHub Desktop.
Adding Types
#lang racket
(define (attach-tag tag item)
(cons tag item))
(define (contents tagged-item)
(cdr tagged-item))
...
(define (bob-width box)
(car (cdr (contents box))))
(define (bob-height box)
(cdr (cdr (contents box))))
...
(define (alice-width box)
(abs (- (car (cdr (contents box)))
(car (car (contents box))))))
(define (alice-height box)
(abs (- (cdr (cdr (contents box)))
(cdr (car (contents box))))))
...
(define (bob-box? box)
(if (= (car box) 'bob-box) true false))
(define (alice-box? box)
(if (= (car box) 'alice-box) true false))
(define (width box)
(cond ((bob-box? box) (bob-width box))
((alice-box? box) (alice-width box))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment