- Imagine trying to determine if there is a doll on our list
(we can call the program contain-doll?)
- We start with our contract header and purpose statement:
;; contains-doll? : list-of-symbols --> boolean
;; to determine whether the symbol 'doll occurs ;on a-list-of-symbols
(define (contains-doll? a-list-of-symbols) ..)
- Let's look at some examples
(contains-doll? empty)
= false
(contains-dolls? cons 'ball empty))
= false
(contains-dolls? cons 'doll empty))
= true
(contains-doll? (cons 'bow (cons 'arrow (cons 'ball empty))))
= false
(contains-doll? (cons 'bow (cons 'arrow (cons 'ball empty))))
= false
- Let's look at the two kinds of cases:
- an empty list
- a constructed list
- This makes our template:
(define (contains-doll? a-list-of-symbols)
(cond
[(empty?) a-list-of-symbols) ...]
[(cons? a-list-of-symbols) ...]))
- or we can use an else clause
(define (contains-doll? a-list-of-symbols)
(cond
[(empty?) a-list-of-symbols) ...]
[else ...]))
- Let's remember that a list can be
viewed as a structure of an item followed by a list:
(define (contains-doll? a-list-of-symbols)
(cond
[(empty?) a-list-of-symbols) ...]
[else ...(first a-list-of-symbols) ... (rest ...a-list-of-symbols) ...]))
- If the list is empty, then contains-doll? must be false.
- If the list is not empty and the first item is a doll,
then contains-doll? must be true.
define (contains-doll? a-list-of-symbols)
(cond
[(empty?) a-list-of-symbols) false]
[else
(cond
[(eq? (first a-list-of-symbols) 'doll) true]
[(rest ...a-list-of-symbols) ...])]))
- What if the non-empty list doesn't begin with doll? Let's
use a program that can check the remaining list - that
program is contains-doll?, the program we
already have!
(define (contains-doll? a-list-of-symbols)
(cond
[(empty?) a-list-of-symbols) false]
[else
(cond
[(eq? (first a-list-of-symbols) 'doll)
true]
[(rest a-list-of-symbols)
(contains-doll? a-list-of-symbols))
]
)
]
)
)
Data Analysis & Design |
To formulate a group of data definitions
|
Develop a data definition for mixed data with at least 2 alternatives, one which can't be self-referential
|
Contract Header & Purpose
|
To name the program, to specify its inputs & outputs,
to describe its purpose, to formulate a program header
|
name the program, its input, outputs, and specify its purpose:
;; name : in1 in2.. -> out
; to compute out
(define (name x1 x2 ..) ..)
|
Examples
|
To characterize the input-output relationship via examples
|
Create examples of the input-output relationship, with at least one example per alternative.
|
Template |
To formulate an outline for a group of programs
|
Develop a cond-expression with one clause per alternative
One alternative cannot refer to the definition.
Have at least one example per alternative
|
Body |
To define the program |
Formulate a Scheme expression for each simple cond-line.
Explain for all other cond-clauses what each natural recursion will compute.
Formulate an answer using selectors, parameters, etc. |
Test |
To expose mistakes ("typos" and logic) |
Apply the program to examples and check against the expected output.
|