Docsity

CS421 Machine Problem Set 1 Solutions: Scheme Programming Languages, Assignments of Computer Science

Toggle sidebar
bg1
UIUC, Department of Computer Science Spring 2006
CS421 Programming Languages Machine Problem Set 1 Solutions
Machine Problem Set 1 Solutions
Assigned: January 24, 2006
Due: January 31, 2006
Objectives and Background
This MP is meant to give you practice writing and using higher order functions. In this
assignment, you may not use any higher order functions provided by scheme, such as length
or map. You may, however, write helper functions. Upon completion of this MP, you should
understand:
•The basics of Scheme
•Recursive functions
•Higher order functions
•Induction
Problem 1
Write a hello world program in Scheme.
(define (hello world )"Hello world!")
>(hello world )
"Hello world!"
Problem 2
Implement your own list length function. It should return the number of elements in the
list.
(define mylist (list −1 0 1 2 3 ))
(define (mylength l)
(if (null? l )
0
(+ 1(mylength (cdr l)))))
1
pf3
pf4
pf5

Partial preview of the text

Download CS421 Machine Problem Set 1 Solutions: Scheme Programming Languages and more Assignments Computer Science in PDF only on Docsity!

UIUC, Department of Computer Science Spring 2006 CS421 Programming Languages Machine Problem Set 1 Solutions

Machine Problem Set 1 Solutions

Assigned: January 24, 2006 Due: January 31, 2006

Objectives and Background

This MP is meant to give you practice writing and using higher order functions. In this assignment, you may not use any higher order functions provided by scheme, such as length or map. You may, however, write helper functions. Upon completion of this MP, you should understand:

  • The basics of Scheme
  • Recursive functions
  • Higher order functions
  • Induction

Problem 1

Write a hello world program in Scheme.

(define (hello world) "Hello world!")

(hello world) "Hello world!"

Problem 2

Implement your own list length function. It should return the number of elements in the list.

(define mylist (list −1 0 1 2 3 )) (define (mylength l) (if (null? l) 0 (+ 1 (mylength (cdr l)))))

(define mylist (list −1 0 1 2 3 )) (mylength mylist) 5

Problem 3

Implement the Fibonacci function f ib(n) that runs in O(n) time: that is, the computation time required to compute f ib(n) grows linearly with n. The Fibonacci function is defined as follows:

f ib(n) :=

0 if n = 0; 1 if n = 1; f ib(n − 1) + f ib(n − 2) if n > 1.

(define (fib n) (letrec ((fibinner (lambda (n) (cond ((< n 0 ) (list 0 0)) ((= n 1 ) (list 1 0)) (else (let ((lastIter (fibinner (− n 1 )))) (list (+ (car lastIter ) (cadr lastIter )) (car lastIter )))))))) (car (fibinner n))))

(fib 7 ) 13 (fib 0 ) 0 (fib 1 ) 1

It is also possible to write this function as a tail-recursive function which also runs in O(n) time. We will talk about tail-recursive functions later in the course.

Problem 4

Brownie Points Problem^1

Can you implement the Fibonacci function that runs in O(log(n)) time? (^1) Brownie Points are the kind of points that have no tangible value, but you still feel good when you get

them. Besides, the solutions to these may be useful to you later in the course or in your CS career. To clarify, Brownie Point problems will not be graded and will have no effect on your grade.

Problem 5

Implement our own reverse function called myreverse. myreverse function should take a list of data, and reverse it.

(define (myreverse l) (letrec ((myreverseinner (lambda (l rl) (if (null? l) rl (myreverseinner (cdr l) (cons (car l) rl)))))) (myreverseinner l ’())))

(myreverse (list 1 2 3 4 5)) (5 4 3 2 1)

Problem 6

Implement your own map function called mymap. mymap should take a function and a list as arguments, and apply the function to each element of the list, returning a list of results. So the ith^ item in the returned list is the result of applying the function to the ith^ item in the passed-in list.

(define (mymap f l) (if (null? l) ’() (cons (f (car l)) (mymap f (cdr l)))))

(mymap fib (list −1 0 1 2 3 7 )) (0 0 1 1 2 13)

Problem 7

Implement the funmap function. funmap should take a list of functions and a data list as arguments, and apply each function from the list of functions to the data list, returning a list of results. So the ith^ item in the returned list is the result of applying the the ith^ function to the data list.

(define (funmap flist l) (if (null? flist) ’() (cons ((car flist) l) (funmap (cdr flist) l))))

(funmap (list powerset mylength myreverse) (list 7 8 9)) (((7 8 9) (7 8) (7 9) ( 7 ) (8 9) ( 8 ) ( 9 ) ()) 3 (9 8 7))

Problem 8

Implement the powerset function. The powerset function takes a list of objects as an ar- gument, and returns a list of all possible subsets of that list. The subsets themselves are represented as lists.

(define (powerset l) (if (null? l) (list l) (append (mymap (lambda (x ) (cons (car l) x )) (powerset (cdr l))) (powerset (cdr l)))))

(powerset (list 0 1 2)) ((0 1 2) (0 1) (0 2) ( 0 ) (1 2) ( 1 ) ( 2 ) ())

Problem 9

Brownie Points Problem

What is wrong with the following proof by induction?

Theorem: All horses are the same color.

Proof: by induction on the number n of horses.

Base Case: n = 1. One horse is always the same color as itself.

Induction Hypothesis: Assume the statement is true for n − 1 horses.

Induction Step: Consider a set S of n horses. Let a be one of the horses, and let S = S 1 ∪ S2 , where S1 and S2 each has n − 1 horses and each contains horse a. By the induction hypothesis, all horses in S1 are the same color as horse a and all horses in S2 are the same color as horse a. So all horses in set S have the same color.

Try to prove it for the case of n = 2. We assume that the statement holds for n = 1. Then a is the only horse. So S1 = {a}, S2 = {a} and S = S 1 ∪ S2 = {a}! The induction step does not prove the statement for n = 2. What happened? The moral of the story is: when you write the induction step, make sure that it does what you want it to do at each step from n all the way to the base case. In this case, the