Docsity

CSci 169 - Software Paradigms Homework 4 - Reversing Lists in Scheme, Assignments of Computer Science

Toggle sidebar
bg1
The George Washington University
School of Engineering and Applied Science
Department of Computer Science
CSci 169 – Software Paradigms – Fall 2007
Homework #4
Due Date: Monday, December 3, 2007
Instructor: A. Bellaachia
Problem 1: (20 points)
Do problem 2 on page 330 of your textbook.
Problem 2: (20 points)
Do problem 3 on page 330 of your textbook.
Problem 3: (20 points)
The Scheme function reverse described in the lecture reverses only the “top level” of a list:
If L =((2 3) 4 (5 6)) then (reverse L) will return ((5 6) 4 (2 3)). Write a Scheme function
deep-reverse that also reverses all sub-lists: (deep-reverse L) will return ((( 6 5) 4 (3 2)).
Problem 4: (20 points)
What does the following scheme function do? Explain.
(define (mystery toto)
(cond ((null? toto) 0)
((not (list? (car toto)))
(cond ((eq? (car toto) '()) (mystery (cdr toto)))
(else (+ 1 (mystery (cdr toto))))))
(else ( + (mystery (car toto)) (mystery (cdr toto))))))
Problem 5: (20 points)
Write a Scheme function that tests for the structural equality of two given lists.
Two lists are structurally equal if they have the same list structure, although their
atoms may be different.

Partial preview of the text

Download CSci 169 - Software Paradigms Homework 4 - Reversing Lists in Scheme and more Assignments Computer Science in PDF only on Docsity!

The George Washington University School of Engineering and Applied Science Department of Computer Science CSci 169 – Software Paradigms – Fall 2007 Homework #

Due Date: Monday, December 3, 2007

Instructor: A. Bellaachia

Problem 1: (20 points) Do problem 2 on page 330 of your textbook.

Problem 2: (20 points) Do problem 3 on page 330 of your textbook.

Problem 3: (20 points) The Scheme function reverse described in the lecture reverses only the “top level” of a list: If L =((2 3) 4 (5 6)) then (reverse L) will return ((5 6) 4 (2 3)). Write a Scheme function deep-reverse that also reverses all sub-lists: (deep-reverse L) will return ((( 6 5) 4 (3 2)).

Problem 4: (20 points) What does the following scheme function do? Explain.

(define (mystery toto)

(cond ((null? toto) 0)

((not (list? (car toto)))

(cond ((eq? (car toto) '()) (mystery (cdr toto)))

(else (+ 1 (mystery (cdr toto))))))

(else ( + (mystery (car toto)) (mystery (cdr toto))))))

Problem 5: (20 points) Write a Scheme function that tests for the structural equality of two given lists. Two lists are structurally equal if they have the same list structure, although their atoms may be different.