CSCI 2041 Week 3 notes
CSCI 2041 Week 3 notes CSCI 2041
U of M
Popular in Advanced Programming Principles
verified elite notetaker
Popular in Computer Science and Engineering
This 3 page Class Notes was uploaded by Lauren Arbisi on Friday September 23, 2016. The Class Notes belongs to CSCI 2041 at University of Minnesota taught by James Moen in Fall 2016. Since its upload, it has received 99 views. For similar materials see Advanced Programming Principles in Computer Science and Engineering at University of Minnesota.
Reviews for CSCI 2041 Week 3 notes
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 09/23/16
Written by Lauren Arbisi Week 3Notes CSCI 2041 Fall 2016 James Moen Monday, September 19, 2016 1. CORRECTION FROM LAST WEEK: (rest ‘( )) returns( ) 2. (first nil) returns nil. (rest nil) returns( ) 3. Cons returns an object called a seq. A seq is a generalized sequence which supports first and rest functions. a. (seq? x) tests if x is a seq. Returns true if x is a seq. Returns false if x is not a seq. b. (seq x) turns x into a seq if possible. If not possible returns an error. c. Following example tests to see if something passed to nonempty? is a filled seq. (def nonempty? (fn [elements] (and (seq? elements) (not (empty? elements))))) d. Where f and r are some objects, and e is a seq: (first (cons f r)) returns f. (rest (cons f r)) returns r. (cons (first e) (rest e)) returns e. e. More examples using cons: (cons ‘a ‘(b c)) returns(a b c) (cons ‘a ‘( )) returns (a) (cons ‘(a b) ‘(c d) returns((a b) c d) (cons ‘( ) ‘(c d)) returns(( ) c d) (cons ‘( ) ‘( )) returns(( )) keep in mind this is differen( )rom f. Example of making a function that utilizes cons. (def replace-by (fn [old new elements] (if (empty? elements) elements (if (= old (first elements)) (cons ;first recursive case new (replace-by old new (rest elements))) (cons ;second recursive case (first elements) (replace-by old new (rest elements))))))) (replace-by a ‘b ‘( )) ;returns ( ) (replace-by ‘a ‘b ‘(c d)) ;returns (c d) (replace-by ‘a ‘b ‘(a b c a d) ;returns (b b c b d) Wednesday, September 21, 2016 1. cond lets you do nested ifs without as many parentheses. 2. Eager evaluation: always evaluate as much of an expression as possible. This is what most (boring) programming languages do. Written by Lauren Arbisi 3. Lazy evaluation: evaluate as little of an expression as possible. What closure does with its lazy list. It is different from short circuit evaluation. 4. The following example is written in pseudocode: f(i, j) = if i != 0 then j else i infinity( ) = infinite loop f (0, infinity( )) a. Eager evaluation will first evaluate i, then attempt to evaluate j, the infinite loop. Ultimately it cannot return a value. b. Lazy evaluation will evaluate i, see that it equals zero, then return i. 5. With lazy evaluation we can represent infinitely large data structures if we only need to look at a finite part of them. 6. Normal list: versus a lazy list: 7. (seq e) turns e into a sequence that we can access via first and rest functions. (lazy-seq e) turns e into a lazy sequence that we can access via first and rest functions. Example: (def lazy-ints (fn [start] ;(start 1) => (1 2 3 …) (lazy-seq (cons start (lazy-ints (+ start 1)))))) 8. A function and examples of its use. (def take (fn [count elements] (if (= count 0) ‘( ) (cons (first elements) (take (- count 1) (rest elements)))))) ***IN REPL** (def ints (lazy-ints 0)) ►(seq? ints) true ►(first ints) 0 ►(take 5 ints) (0 1 2 3 4) ►(rest ints) ;infinite loop (1 2 3 4 5 6 7…) Written by Lauren Arbisi 9. Fibonacci sequence a. (0 = 0) f1= 1 f2= 1 fn= n-1 fn-2 b. Clojure lazy list function for Fibonacci sequence (def lazy-fib (fn ([ ] (lazy-fib 1 1)) ([a b] (lazy-seq (cons a (lazy-fib b (+ a b))))))) Friday, September 23, 2016 1. Where f is a function and e’s are (map f ‘(e0 e1 ek)) evaluates to((f e0) (f e1) (f ek)) a. Function f is not necessarily called when you think it is. b. (def square (fn [n] (print (str “<” n “> ”)) (* n n))) **IN REPL** ►(square 4) <4> 16 ►(def squares (map square ‘(1 2 3 4))) ;square not called #’user/squares ►squares (<1> <2> 1 <3> 4 <4> 9 16) ;square called ►squares (1 4 9 16) ;square not called In the example above, the value of squares is stored in memory after it is first called. 2. How vars work a. **IN REPL** ►(def k 1) #’user/k ;k is bound to a var, see diagram 1 below ►(def k 2) #’user/k ;k’s value is changed, see diagram 2 below ►k 2 Diagram 1 Diagram 2 b. def only works with globals 3. Namespaces: sets of names **IN REPL** user=> (def x 1) ;user is current namespace, x is within user #’user/x user=> (ns my-namespace) ;changes namespace to my-namespace nil my-namespace=> x ;x doesn’t exist in my-namespace, returns error