1 sort a list of distinct numbers in ascending


(1) Sort a list of distinct numbers in ascending order, using the following divide-and-conquer strategy (Quicksort): divide the list of numbers into two lists: one that contains all items that are strictly smaller than the first item (often called the pivot), and another with all those items that are strictly larger than the first item.

Then the two smaller lists are sorted using the same procedure. Once the two lists are sorted, the pieces are juxtaposed.

(2) Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

(define stack1 (make-stack))  
(define stack2 (make-stack))

Write procedures to manipulate stacks, e.g.

(stack1 'empty?)  ==>  boolean
(stack1 'push! item)  ==> pushes item on top of stack
(stack1 'top)  ==> returns top element of stack, leaves stack unchanged
(stack1 'pop!) ==> throws away top element of stack
(stack1 'print) ==> prints some representation of the stack from top to bottom, enclosed in brackets etc....

Your tests should include making several stacks, pushing on one what is popped from the other, attempts to pop from an empty stack etc.

Also write a procedure to reverse a list by using two stacks.

(3) Implement your own streams.

(a) Write (delay ) as a special form for (lambda () ) and (force ), as discussed in class.

(b) Write (stream-cons x y) as a special form, as discussed in class.

(c) Write stream analogues of some familiar list processing functions, including:

(stream-car str)
(stream-cdr str)
(stream-null? str)
(stream-ref str n) --- returns the nth element in stream str
(stream-filter pred str)    --- makes a new stream of elements satisfying pred
(stream-for-each proc str) --- applies proc to each element of str for side effect
(first n str)    --- makes a stream of the first n items in str
(list->stream lis) --- makes a stream from list lis
(stream->list str) --- opposite coercion

(d) Now define a bunch of streams to test your functions:

(i) an infinite stream of 1's
(ii) an infinite stream of all even integers
(iii) an infinite stream of random numbers between 1 and 100

(iv) write a predicate (prime? n) that tests for primality and use it to create a stream of all primes

(4) (i) Add the special form let to the metacircular interpreter

(ii) What changes are needed in the metacircular interpreter so that Scheme uses dynamic instead of lexical scoping?

Solution Preview :

Prepared by a verified Expert
Computer Engineering: 1 sort a list of distinct numbers in ascending
Reference No:- TGS0442348

Now Priced at $20 (50% Discount)

Recommended (96%)

Rated (4.8/5)