Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Lesson 05 Analyzing Structures


Published on

  • Be the first to comment

  • Be the first to like this

Lesson 05 Analyzing Structures

  1. 1. Analyzing Structures<br />CS 5010 Program Design Paradigms “Bootcamp”<br />Week 03, Lesson 3<br />TexPoint fonts used in EMF. <br />Read the TexPoint manual before you delete this box.: AAA<br />1<br />
  2. 2. Template for recursive/mixed data<br />;; A Foo is one of<br />;; -- (make-foo1 Number FooFoo)<br />;; -- (make-foo2 Foo Boolean)<br />;; -- (make-foo3 Number Boolean)<br />(define-struct foo1 (field1 field2 field3))<br />(define-struct foo2 (field1 field2))<br />(define-struct foo3 (field1 field2))<br />(define (foo-fn f)<br /> (cond<br /> [(foo1? f) (... (foo1-field1 f)<br /> (foo-fn (foo1-field2 f))<br /> (foo-fn (foo1-field3 f)))]<br /> [(foo2? f) (... (foo-fn (foo2-field1 f))<br /> (foo2-field2 f))]<br /> [else (... (foo3-field1 f)<br /> (foo3-field2 f))]))<br />
  3. 3. Which variant do we have?<br />In this case, it's easy– we have predicates<br />Sometimes not so easy<br />for SoS, we had to distinguish between strings and non-strings.<br />Before, we had adam? and eve?<br />Today's topic: How to design predicates<br />
  4. 4. An Example Problem<br />We are given a list of bird observations.<br />Each observation consists of 3 parts:<br /> beak, wings, belly<br />We have a table for converting an observation to a species:<br />
  5. 5. Determining the species<br />
  6. 6. Example Problem: Goal<br />Given a list of observations, we are to produce a list of species.<br />
  7. 7. Simple-Minded Solution: Truth Tables<br />;; observation2species : Observation -> Species<br />;; given an observation, determine the species<br />;; examples/tests: below<br />;; strategy: decision tree (truth table)<br />(define (observation2species obs)<br /> (cond<br /> [(or ;; star-bellied sneetches:<br /> (and (yellow? (observation-beak obs))<br /> (red? (observation-wings obs))<br /> (star? (observation-belly obs)))<br /> (and (black? (observation-beak obs))<br /> (red? (observation-wings obs))<br /> (star? (observation-belly obs)))<br /> (and (black? (observation-beak obs))<br /> (blue? (observation-wings obs))<br /> (star? (observation-belly obs))))<br /> "Star-Bellied Sneetch"]<br /> [(or ;; plain-bellied sneetches:<br /> (and (yellow? (observation-beak obs))<br /> (red? (observation-wings obs))<br /> (plain? (observation-belly obs)))<br /> (and (black? (observation-beak obs))<br /> (red? (observation-wings obs))<br /> (plain? (observation-belly obs))) <br /> (and (black? (observation-beak obs))<br /> (blue? (observation-wings obs))<br /> (plain? (observation-belly obs))))<br /> "Plain-Bellied Sneetch"]<br /> [else "Yellow-Bellied Blackbird"]))<br />
  8. 8. A cleverer approach: a decision tree<br />beak<br />Yellow<br />Black<br />wings<br />Blue<br />Red<br />Yellow-Bellied<br />belly<br />Plain<br />Star<br />Plain-Bellied<br />Star-Bellied<br />
  9. 9. Code using Decision Tree<br />;; observation2species : Observation -> Species<br />;; strategy: decision tree<br />(define (observation2species obs)<br /> (if (yellow? (observation-beak obs))<br /> (if (black? (observation-wings obs))<br /> "Yellow-Bellied Blackbird"<br /> (not-ybb-to-species obs))<br /> (not-ybb-to-species obs))) <br />;; not-ybb-to-species : Observation -> Species<br />;; Given an observation THAT IS KNOWN NOT TO BE A YBB<br />;; determine which species of sneetch it is.<br />(define (not-ybb-to-species obs)<br /> (if (plain? (observation-belly obs))<br /> "Plain-Bellied-Sneetch"<br /> "Star-Bellied-Sneetch"))<br />A side-condition like this is called an INVARIANT<br />
  10. 10. Decision Trees vs. Truth Tables<br />Truth table code is boring, but reliable<br />Decision Tree: may take effort to get right!<br />Testing is vital!<br />Decision Tree: shorter<br />Have to figure out order of tests<br />Decision Tree: sometimes necessary<br />what if you couldn't see the blackbird's belly?<br />this happens often in practice.<br />
  11. 11. Summary<br />Use wishlist functions to discriminate between cases.<br />Use truth tables or decision trees to figure out what's what.<br />Test, test, test!<br />