1 / 9

Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types

Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types. Recall inductive BNF definition of binary trees: <bintree> ::= <number> | (<symbol> <bintree> <bintree>) To write programs using bintree, we'll need:. constructors for different kinds of bintrees (like Java)

metta
Télécharger la présentation

Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: Data Abstraction2.2 An Abstraction for Inductive Data Types • Recall inductive BNF definition of binary trees: • <bintree> ::= <number> | (<symbol> <bintree> <bintree>) • To write programs using bintree, we'll need: • constructors for different kinds of bintrees (like Java) • a predicate for telling whether an object is a bintree • (unlike Java, because Scheme types are dynamic) • a way of telling whether bintree is leaf or interior • a way of getting out the leaf or interior component

  2. An Abstraction for Inductive Data Types • Tell Dr. Racket to use the language extension from Essentials of Programming Languages (EOPL) textbook:

  3. An Abstraction for Inductive Data Types • Now we can use the define-datatype special form: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) • What is this doing?

  4. Defines a datatype bintree, with predicate bintree? • (define-datatype bintree bintree? • A bintree is either • a leaf node consisting of a datum, which is a number • (leaf-node • (datum number?)) • an interior node consisting of a key that is a symbol, a left child that is a bintree, and a right child that is a bintree (interior-node (key symbol?) (left bintree?) (right bintree?)))

  5. Creates a dataype with the following interface: • (define-datatype bintree bintree? • A 1-argument procedure for constructing a leaf node, with a test to make sure that the argument is a number • (leaf-node • (datum number?)) • A 3-argument procedure for building an interior node, with a test for the first arg being a symbol and the second and third being a bintree (interior-node (key symbol?) (left bintree?) (right bintree?)))

  6. General format: (define-datatypetype-nametype-predicate-name {(variant-name {(field-namepredicate)}*}+) More on define-datatype (define-datatype bintree bintree? (leaf-node (datum number?))

  7. Determining variants via cases (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (datum) datum) (interior-node (key left right) (+ (leaf-sum left) (leaf-sum right)))))) (Essentially, a pattern-matcher)

  8. Determining variants via cases Supports type-checking: > (define tree-a (interior-node 'a (leaf-node 2) (leaf-node 3))) > (leaf-sum tree-a) 5 > (leaf-sum 'foo) case: not a bintree: foo

  9. Determining variants via cases General format: (casestype-nameexpression {(variant-name({field-name}*)consequent)}* (cases bintree tree (interior-node (key left right) (+ (leaf-sum left) (leaf-sum right))))))

More Related