posted Oct 29, 2015, 4:52 AM by Samuel Konstantinovich   [ updated Oct 30, 2015, 6:24 AM ]
Exam Next Friday November 6th

Goal: Manipulating Lists

I. Determining how cons/list work differently.

You have 11 minutes to explore the difference between the two commands:
1. list 
2. cons

the PARAMETERS are capitalized, so 
LIST: any literal or variable list you give it like '(1 2 3) or L if L contains a list. 
ELEMENT: can be any Atom, literal or variable

(cons  LIST1 LIST2)  
(list LIST1 LIST2)   

Test these two functions on different values, so that you feel confident explaining how it works. This means you will come up with Several test cases for each, so that you can predict future behavior. 

This is an experiment you are conducting to determine how something works. Make sure you test it on different kinds of lists, and non-lists.

Exam Next Friday November 6th

II. Applications of cons:

We can use cons in our recursion to do something we couldn't do before...

1. (makeList n) -> creates the list (n   n-1   n-2 .... 2 1 0)
(makeList 5) -> (5 4 3 2 1 0)

Can we reverse the order? What would be required?

2. Make list of perfect squares
(makeSquares n) ->( n^2  (n-1)^2   ...  2^2    1^2)
(makeSquares 5) -> (25 16 9 4 1)

Can we reverse the order? 

Exam Next Friday November 6th

;Code from class:
;We can build a list with cons as follows:
;(cons 2(cons 1 (cons 0 '())))

;assume n >= 0
(define makeList (lambda (n)
                   (if (= n 0)
                       (cons n (makeList (- n 1))))))
(makeList 5)
(define makeListIncreasing (lambda (n)
                           (makeListAtoB 0 n)))

;Required to add a new parameter so we can keep track of 
;Both the current value and the ending value. 
(define makeListAtoB (lambda (a b) (if (= a b) (list a);(cons limit '()) (cons a (makeListAtoB (+ 1 a) b ))))) (makeListIncreasing 5)