6.001 Tutorial 3 NotesGerald Dalley22 Feb 2005Orders of Growth ReviewThe simplest way of determining order of growth isto figure out how much extra work is necessary whenyou change the size of the problem. A few rules youcan generally use to solve by inspection (n is the sizeof the old problem, n0is the size of the new problem):• If n0= n − C, the order is Θ(n).• If n0=nC, the order is Θ(log n).• If n0= (n − C) + (n − C), the order is Θ(2n).The let Special Formlet is a piece of syntactic sugar to allow us to createtemporary local variables. We do transformations ofthe form:(let ((a 2)(b (+ 1 2)))(+ a b))((lambda (a b) (+ a b))2(+ 1 2))Rewrite(define (greatest-value x y)(max (+ x y) (- x y)))by using the let special form instead of the maxprocedure:( d e f i n e ( g r ea t e st − va l u e x y )( l e t ( ( a ( + x y ) )( b (− x y ) ) )( i f ( a > b )ab ) ) )Pairs and ListsThe cons procedure creates a cons cell (also knowsas a pair), which has two elements, the car and cdr.The car and cdr can both have any type of value. Wedraw pairs as two boxes stuck together, where thefirst box is the car, the second is the cdr. We drawarrows out of the boxes to show what values theyhave – always drawing a down arrow out of the car,a right arrow out of the cdr.We can chain cons cells together to create lists. Alist is a set of cons cells where the cdr of one pointsto the next, and the cdr of the last cons cell points tonil, the special value that means the empty list. Forvarious (sometimes) useful reasons, our implementa-tion of Scheme defines nil to be the same as false –nil, (), and #f all mean exactly the same thing.The basic procedures that you need to understandand be familiar with are cons, car, cdr, and list.For the most part, we don’t care about dotted pairs,so cons takes something and a list (possibly empty),and sticks that something on the front of the list. cartakes a non-empty list and returns the first thing init; cdr returns everything but the first thing. listtakes any number of things and returns a list of themall.Boxes and PointersFor the following problems, give the box-and-pointerrepresentation and the printed representation.(cons 1 2)1 2(1 . 2)(cons 1 (cons 3 (cons 5 nil)))531(1 3 5)1(cons (cons (cons 3 2) (cons 1 0)) nil)3 21 0(((3 . 2) 1 . 0))(cons 0 (list 1 2))210(0 1 2)(list (cons 1 2) (list 4 5) 3)1 2 543((1 . 2) (4 5) 3)Write Scheme co de that would produce the follow-ing printed representations.(1 2 3)(list 1 2 3)(1 2 . 3)(cons 1 (cons 2 3))((1 2) (3 4) (5 6))(list (list 1 2) (list 3 4) (list 5 6))Practice Problems; ; This pr o c edure r et u rn s t he l e n g t h; ; ( i . e . number o f e l ements ) in a l i s t( d e f i n e ( length l s t )( i f ( nu ll ? l s t )0( + 1 ( length ( c d r l s t ) ) ) ) )Time = Θ(n) , Space = Θ(n) , n = length of lst; ; This pr o c edure r et u rn s t he nth; ; e lemen t o f a l i s t , where t h e f i r s t; ; e lemen t has i n d e x 0( d e f i n e ( l i s t − r e f l s t n )( i f (= n 0 )( c a r l s t )( l i s t − r e f ( cdr l s t ) (− n 1 ) ) ) )Time = Θ(n) , Space = Θ(1) , n = n; ; This pr o c edure j o i n s two l i s t s t o g e t h e r; ; e . g . ( append ( l i s t 1 2 ) ( l i s t 3 4 ) ); ; g i v e s ( 1 2 3 4 )( d e f i n e ( append l s t 1 l s t 2 )( i f ( null ? l s t 1 )l s t 2( cons ( c a r l s t 1 )(append ( cd r l s t 1 ) l s t 2 ) ) ) )Time = Θ(n) , Space = Θ(n) , n = length of lst1; ; This pr o c edure r e v e r s e s t h e o r der o f; ; e l e m e n t s in a l i s t append may be u s e f u l( d e f i n e ( reverse l s t )( i f ( null ? l s t )n i l(append ( reverse ( c d r l s t ) )( l i s t ( c a r l s t ) ) ) ) )Time = Θ(n2) , Space = Θ(n) , n = length of lst; ; Swaps the f i r s t and sec ond i t e m s i n; ; a l i s t t h a t has a t l e a s t 2 items; ; ( swap− first− and− second ( l i s t 1 2 3 ) ); ; ; Value : ( 2 1 3 )( d e f i n e ( swap− first− and− second l s t )( cons ( cadr l s t ) ( cons ( c ar l s t ) ( cd dr l s t ) ) )Time = Θ(1) , Space = Θ(1) , n = length of lstHOPsHigher-order procedures are procedures that eitheraccept procedures as arguments or return proceduresas their values.; ; This pr o c edure a p p l i e s f t o each; ; e lemen t o f t he l i s t , and r e t u r ns a; ; new l i s t made from th o s e v a l u e s( d e f i n e (map f l s t )( i f ( null ? l s t )n i l( cons ( f ( ca r l s t ) )(map f ( cdr l s t ) ) ) ) )Time = Θ(nΘf(·)) , Space = Θ(nΘf(·)) ,n = length of
View Full Document