Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
Com S 342 --- Principles of Programming Languages
HOMEWORK 2: INDUCTION and RECURSION
Due: problems 1-7, 9 on Tuesday, February 12, 2008.
In this homework, you will learn about higher-order procedures, syntax
abstraction, and a bit more about recursion.
All code is to be written in Scheme, using the exact procedure names
specified in the problem descriptions. Test using your own tests and
also use our tests, as described in homework 1. For coding
problems hand in:
- a printout of the code, and
- if our tests reveals any errors with your code for that problem,
then include a transcript showing a run of our tests.
That is, you only have to hand in output of your testing if it reveals
problems in your code (that you have not fixed). We expect you to run
our tests for each problem, but since the output in the case where the
tests all pass is not very interesting, we will trust you to have done
this. However, you must hand in a transcript of your testing if your
code does *not* pass our tests perfectly, as this will alert us to the
problem and will help us assign partial credit. If we find that your
code is not correct and the problem would have been revealed by our
testing, but you did not hand in test results showing
these problems, then you will receive 0 points for that problem.
You may also hand in a transcript that includes our tests if you wish.
Tests are in the directory $PUB/homework/hw2 for test data for each
coding problem, as described in homework 1. (Recall that $PUB
means /home/course/cs342/public/.) This applies even if you aren't
using the Com S machines. (If you're not, see the course web page for
how to test at home.)
The section headings below give the readings related to the problems.
(Please read them!)
STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS, SECTION 1.3
(http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3)
1. (5 points) [vol-cuboid]
Define a curried version of the following procedure,
call it vol-cuboid.
>(define (vol-cuboid l b h)
(* l b h))
(vol-cuboid 2 3 6)
Test your code by executing the following.
(test-hw2 "vol-cuboid")
You can check the syntax by using the "Check Syntax"
button in DrScheme.
Remember, for this and all of the following coding problems, you
must hand in both the printout of your code file and a transcript.
Be sure to include statements at the top of your code file
identifying yourself, as described in previous homeworks.
2. (5 points) [vol-4-5]
Using your solution from problem 1, define a procedure,
vol-4-5
that takes a number, h, and returns the volume of the cuboid
with length 4, width 5 and height h. For example,
(vol-4-5 2) ==> 40
(vol-4-5 3) ==> 60
Test your code by executing the following.
(test-hw2 "vol-4-5")
Important: your solution must use vol-cuboid from the previous
problem. You will get zero points if your solution's code does not
depend on vol-cuboid.
STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS, SECTIONS 1.1.6
(http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.6)
REVISED^5 REPORT ON THE ALGORITHMIC LANGUAGE SCHEME, section 4.2
CODE EXAMPLES PAGE
(http://www.cs.iastate.edu/~cs342/lib342/code-examples.html#SyntacticAbstraction)
3. (10 points) [occurs-twice? using "and" and "or"]
Write the procedure occurs-twice? from homework 1, problem 5 without
using #t and #f, but using "and" and "or".
Test your code by executing the following:
(test-hw2 "occurs-twice")
4. (5 points) [example of syntactic sugar]
Give an example of a statement or expression in Java or C++ that is a
syntactic sugar. State what language you are giving an example
from. Explain by a simple example how to desugar this statement or
expression.
THE LITTLE SCHEMER, Chapters 2-5
FOLLOWING THE GRAMMAR
(handout at http://www.cs.iastate.edu/~cs342/docs/follow-grammar.pdf)
5. (20 points) [Following the grammar for flat lists]
For each of the following, say whether the procedure has a correct
outline that follows the grammar for flat lists, and if not,
briefly explain what the problem is with that procedure
(why it does not follow the outline).
(Note: you don't have to judge whether these are correct or not,
and you aren't expected to run them.)
;; (a)
(define talents-of
(lambda (people)
(cond
((null? people) '())
(else (cons (talent (car people))
(talents-of (cdr people)))))))
;; (b)
(define talents-of
(lambda (people)
(cons (talent (car people))
(talents-of (cdr people)))))
;; (c)
(define talents-of
(lambda (people)
(cond
((null? people) '())
(else (talent (car people))
(talents-of (cdr people))))))
;; (d)
(define talents-of
(lambda (people)
(if (null? people)
(list)
(cons (talent (car people))
(talents-of (cdr people))))))
;; (e)
(define talents-of
(lambda (people)
(if (null? people)
(list)
(begin (cdr people)
(define first-talent (car people))
(cons (talent first-talent)
(talents-of people))))))
;; (f)
(define talents-of
(lambda (people)
(if (null? people)
people
(cons (talent (car people))
(talents-of (cdr people))))))
;; (g)
(define talents-of
(lambda (people)
(cond
((null? people) '())
(else (talents-of (talent (car people))
(cdr people))))))
6. (20 points) [Following the grammar for flat lists 2]
For each of the following, say whether the procedure has a correct
outline that follows the grammar for flat lists, and if not,
briefly explain what the problem is with that procedure
(why it does not follow the outline).
(Note: you don't have to judge whether these are correct or not,
and you aren't expected to run them.)
;; (a)
(define rhymes-with
(lambda (sought words)
(if (null? words)
'()
(append
(if (not (rhyme? sought (car words)))
'()
(list (car words)))
(rhymes-with sought (cdr words))))))
;; (b)
(define rhymes-with
(lambda (sought words)
(if (null? words)
(list)
(if (not (rhyme? sought (car words)))
(rhymes-with sought (cdr words))
(cons (car words)
(rhymes-with sought (cdr words)))))))
;; (c)
(define rhymes-with
(lambda (sought words)
(cond
((null? words) '())
(else (cond
((not (rhyme? sought (car words)))
(rhymes-with sought (cdr words)))
(else (cons (car words)
(rhymes-with sought (cdr words)))))))))
;; (d)
(define rhymes-with
(lambda (sought words)
(cond
((null? words) '())
(else (cdr words)
(cond
((not (rhyme? sought (car words)))
(rhymes-with sought words))
(else (cons (car words)
(rhymes-with sought words))))))))
;; (e)
(define rhymes-with
(lambda (sought words)
(cond
((rhyme? sought (car words))
(cons (car words)
(rhymes-with sought (cdr words))))
(else
(rhymes-with sought (cdr words))))))
;; (f)
(define rhymes-with
(lambda (sought words)
(cond
((null? words) words)
(else (cond
((not (rhyme? sought (car words)))
(rhymes-with sought (cdr words)))
(else (cons (car words)
(rhymes-with sought (cdr words)))))))))
;; (g)
(define rhymes-with
(lambda (sought words)
(cond
((null? words) '())
((rhyme? sought (car words))
(cons (car words)
(rhymes-with (cdr words))))
(else
(rhymes-with cdr words)))))
ESSENTIALS OF PROGRAMMING LANGUAGES: pages 1-19 (you can skim section 1.1)
THE LITTLE SCHEMER, CHAPTER 3
CODE EXAMPLES WEB PAGE,
http://www.cs.iastate.edu/~cs342/lib342/code-examples.html#Little2
http://www.cs.iastate.edu/~cs342/lib342/code-examples.html#Little3
FOLLOWING THE GRAMMAR HANDOUT
http://www.cs.iastate.edu/~cs342/docs/follow-grammar.pdf
This first set of problems is about recursion over flat lists
7. (5 points) [remove-second]
Write a procedure, remove-second based on the procedure remove-first
in the text (which means the Essentials of Programming Languages,
second edition book). The following are examples of applying
remove-second.
(remove-second (quote hello) '())
==> ()
(remove-second 'hello '(I am saying hello to world hello world))
==> (I am saying hello to world world)
(remove-second '2 '(1 2 3 2 4 5))
==> (1 2 3 4 5)
After doing your own testing, you must test your code using:
(test-hw2 "remove-second")
Important: your solution must use remove-first from the text.
You will get zero points if your solution's code does not
depend on remove-first.
8. (suggested practice) [change to remove]
Do exercise 1.9 on p. 19 in the text.
9. (10 points) [append-all]
Write a procedure, append-all, whose type is given by the following
deftype:
(deftype append-all
(forall (T)
(-> ((list-of (list-of T))) (list-of T))))
The procedure append-all takes a list of lists, ll, and returns a
list of all the lists in ll appended together in the same order in
which they appear in ll. In your solution's code you may only use
Scheme's append procedure with two (2) arguments.
The following are examples.
(append-all '())
==> ()
(append-all '((3 7 10) (20 25 30) (5 4 3)))
==> (3 7 10 20 25 30 5 4 3)
(append-all '((20 25 30) (5 4 3)))
==> (20 25 30 5 4 3)
(append-all '((l i k) (e) (t h i s) () (o k)))
==> (l i k e t h i s o k)
After doing your own testing you must test your code using:
(test-hw2 "append-all")