Download Extending Skills Program 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Bra–ket notation wikipedia , lookup

History of the function concept wikipedia , lookup

Big O notation wikipedia , lookup

Elementary mathematics wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Transcript
Extending Skills Program 1- Lists of Lists
Use the Beginning Language with List Abbreviations to do the following problem:
An SB is a list of two items: (cons String (cons Boolean empty))
An SB-list (Symbol – Boolean list) is either
- empty
- (cons SB SB-list)
Develop the function which-toppings, which consumes an SB-list of ice-cream toppings and
produces the list of strings that are associated with true.
You must fully complete all parts of the design recipe for credit. Please submit on paper.
;Example
(which-toppings (cons (cons “Hot fudge” (cons true empty))
(cons “Ketchup” (cons false empty)) empty ))
“should be (cons (cons “Hot Fudge (cons true empty)) empty)
ExtendingSkills-Prog 2
Expression Trees
Concepts
Activity
Homework
Review and Extension
Concepts
Recall that this is a ftree1, a binary family tree of ancestors.
Recall the definition for a ftree1:
A family tree I (ftree1) is either
'unknown, or
a structure
(make-ftree1 n left right)
where
n is a symbol for a name,
and left and right are **ftree1's.
Here are some more important operations on trees, and indeed, on any sort of data structure.
One is called traversal. That means "visiting" every node in a certain order.
Many programming tasks involve visiting every item in a data structure: adding them up,
printing them out, searching for all occurances of a certain value or values that have a certain
property.
When we had a linear list
There was basically only one way to visit each node: do it in order.
With trees however there are 3 strategies: preorder traversal, inorder traversal, and postorder
traversal.
To (preorder-traverse T):
Visit (ftree1-root T),
then
(preorder-traverse (ftree1-left T)),
and then (preorder-traverse (ftree1-right T))
So a preorder traversal is : lassie, sam, lad, lady, kate, buck, missy.
To do inorder traversal, visit the root second:
To (inorder-traverse T):
First (inorder-traverse (ftree1-left T)),
then visit (ftree1-root T),
and then (inorder-traverse (ftree1-right T))
So an inorder traversal is: lad, sam, lady, lassie, buck, kate, missy.
To do postorder traversal, visit the root last:
To (postorder-traverse T):
First (postorder-traverse (ftree1-left T)),
then (postorder-traverse (ftree1-right T))
then visit (ftree1-root T).
And a post-order traversal is lad, lady, sam, buck, missy, kate, lassie.
Which is sort of interesting, but not very useful with dog pedigrees.
So, let's see an example where these distinctions are really meaningful
.Suppose we have a different kind of tree, one where we represented an expression.
Each node is an operator or a number. (The leaves are all numbers).
*
/ \
+
*
/ \ / \
2 3 4 5
We could print it in prefix notation by using a preorder traversal and printing at each node:
* + 2 3 * 4 5
We could print it in normal notation using an inorder traversal, but we'd have to add parentheses
to be unambiguous:
(2+3)* (4*5) = 100
[Note that: 2+3*4*5 = 62 ]
We could evaluate the expression it represented by doing a postorder traversal. At every operator
node, we find the value of the operands, and then do the operation.
2 3 + 4 5 * *
or in other words:
2, 3, add to get 5, 4, 5, multipy to get 20, multiply to get 100
(Note that preorder and postorder really are different; they aren't just mirror images).
Activity
Start a file called ExtSkillsPrg2 with name, date as usual.
1) Write a data definition for an expression as in the text above. Call it exp. An exp is either a
number or a structure.
1a) Write the define-struct for the expression, and a sufficient set of sample data. Here's one for
free.
(define test1 (make-exp '* (make-exp '+ 2 3) (make-exp '* 3 4)))
2) Write the template for an exp function. Hint: there's a function called number? and recall
that define-struct will give you exp?
3) Use the template to write a function called evaluate that consumes an exp function and
evaluates it. If the data field is '+, evaluate the operands, and then add them, and so forth.
This program is courtesy of:
http://weber.marblefalls.txed.net/mfhs/academics/electives/compsci/projects.htm