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

The instructions and problems for homework 4 of the csci 169 - software paradigms course offered by the george washington university's department of computer science in the fall of 2007. Students are required to complete problems related to reversing lists in scheme, including writing a deep-reverse function and explaining the behavior of a given mystery function.

Typology: Assignments

Pre 2010

Uploaded on 08/19/2009

koofers-user-8xj
koofers-user-8xj 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

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.