notes19 - Actually were violating the data abstraction when...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
Actually we’re violating the data abstraction when we type in a hand of cards as ’(3h 10c 4d) because that assumes we know how the cards are represented—namely, as words combining the rank number with a one-letter suit. If we want to be thorough about hiding the representation, we need constructor functions as well as the selectors: ;;;;; In file cs61a/lectures/2.1/total.scm (define (make-card rank suit) (word rank (first suit)) ) (define make-hand se) > (total (make-hand (make-card 3 ’heart) (make-card 10 ’club) (make-card 4 ’diamond) )) 17 Once we’re using data abstraction we can change the implementation of the data type without aFecting the programs that use that data type. This means we can change how we represent a card, for example, without rewriting total : ;;;;; In file cs61a/lectures/2.1/total.scm (define (make-card rank suit) (cond ((equal? suit ’heart) rank) ((equal? suit ’spade) (+ rank 13)) ((equal? suit ’diamond) (+ rank 26)) ((equal? suit ’club) (+ rank 39))
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.
Ask a homework question - tutors are online