source: liacs/cvp/assignments/lecture6.txt@ 144

Last change on this file since 144 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
5All quotes are based on the 8th edition of Concepts of programming
6languages from Robert W. Sebesta
7
8*** Chapter 15 Review Questions ***
9Problem Set 6,7
10
116)
12Q: 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)))
18A: It will remove the head of the list till it find s, if none found
19 '() is the result
20
217)
22Q: 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))
32A: It will traverse the lis and count the number of atoms in there
33
34
35Problem Excercises 4,5,6
364)
37Q: Write Scheme function that returns the number of zeros in a given
38 simple list of numbers.
39A:
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
485)
49Q: Write a Scheme function that deletes all top-level instances of an
50 given atom from a given list
51A:
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
606)
61Q: 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.