Last change
on this file since 199 was 2, checked in by Rick van der Zwet, 15 years ago |
Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)
|
File size:
1.6 KB
|
Line | |
---|
1 | # Rick van der Zwet
|
---|
2 | # StudentID: 0433373
|
---|
3 | # $Id: lecture6.txt 339 2007-11-29 22:01:09Z rick $
|
---|
4 |
|
---|
5 | All quotes are based on the 8th edition of Concepts of programming
|
---|
6 | languages from Robert W. Sebesta
|
---|
7 |
|
---|
8 | *** Chapter 15 Review Questions ***
|
---|
9 | Problem Set 6,7
|
---|
10 |
|
---|
11 | 6)
|
---|
12 | Q: What does the following Scheme function do?
|
---|
13 | (define (y s lis)
|
---|
14 | (cond
|
---|
15 | ((null? lis) '() )
|
---|
16 | ((equal? s (car lis)) lis)
|
---|
17 | (else (y s (cdr lis)))
|
---|
18 | A: It will remove the head of the list till it find s, if none found
|
---|
19 | '() is the result
|
---|
20 |
|
---|
21 | 7)
|
---|
22 | Q: What does the following Scheme function do?
|
---|
23 | (define (x lis)
|
---|
24 | (cond
|
---|
25 | ((null? lis) 0)
|
---|
26 | ((not (list? (car list)))
|
---|
27 | (cond
|
---|
28 | ((eq? (car lis) nil) (x (cdr lis)))
|
---|
29 | ((else (+ 1 (x (cdr list))))))
|
---|
30 | (else (+ (x (car list)) (x cdr lis))))
|
---|
31 | ))
|
---|
32 | A: It will traverse the lis and count the number of atoms in there
|
---|
33 |
|
---|
34 |
|
---|
35 | Problem Excercises 4,5,6
|
---|
36 | 4)
|
---|
37 | Q: Write Scheme function that returns the number of zeros in a given
|
---|
38 | simple list of numbers.
|
---|
39 | A:
|
---|
40 | (define (x lis)
|
---|
41 | (cond
|
---|
42 | ((null? lis) 0)
|
---|
43 | ((eq? 0 (car lis)) (+ 1 (x (cdr lis)))
|
---|
44 | (else (+ 0 (x (cdr lis)))
|
---|
45 | )
|
---|
46 | )
|
---|
47 |
|
---|
48 | 5)
|
---|
49 | Q: Write a Scheme function that deletes all top-level instances of an
|
---|
50 | given atom from a given list
|
---|
51 | A:
|
---|
52 | (define (x atom lis)
|
---|
53 | (cond
|
---|
54 | ((null? lis) '() )
|
---|
55 | ((eq? atom lis) (x atom cdr(lis)))
|
---|
56 | ((else (cons car(atom) (x atom cdr(lis)))))
|
---|
57 | )
|
---|
58 | )
|
---|
59 |
|
---|
60 | 6)
|
---|
61 | Q: Write a Scheme function that removes the last element from a given
|
---|
62 | list
|
---|
63 | (define (x list)
|
---|
64 | (cond
|
---|
65 | ((null? cdr(list) '() ))
|
---|
66 | ((else (cons car(list) (x cdr(list)))))
|
---|
67 | )
|
---|
68 | )
|
---|
69 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.