×

### Let's log you in.

or

Don't have a StudySoup account? Create one here!

×

or

## CSCI 2041 Week 3 notes

by: Lauren Arbisi

99

4

3

# CSCI 2041 Week 3 notes CSCI 2041

Lauren Arbisi
U of M

Get a free preview of these Notes, just enter your email below.

×
Unlock Preview

These notes cover seqs, lazy evaluation, lazy lists and, how vars and namespaces work, among a few other small things.
COURSE
PROF.
James Moen
TYPE
Class Notes
PAGES
3
WORDS
CONCEPTS
cse, CSCI, CS, Computer, Science
KARMA
25 ?

## 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

×

×

### What is Karma?

#### You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

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

×

×

### BOOM! Enjoy Your Free Notes!

×

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

## Why people love StudySoup

Bentley McCaw University of Florida

#### "I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"

Amaris Trozzo George Washington University

#### "I made \$350 in just two days after posting my first study guide."

Steve Martinelli UC Los Angeles

#### "There's no way I would have passed my Organic Chemistry class this semester without the notes and study guides I got from StudySoup."

Parker Thompson 500 Startups

#### "It's a great way for students to improve their educational experience and it seemed like a product that everybody wants, so all the people participating are winning."

Become an Elite Notetaker and start selling your notes online!
×

### Refund Policy

#### STUDYSOUP CANCELLATION POLICY

All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email support@studysoup.com

#### STUDYSOUP REFUND POLICY

StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here: support@studysoup.com

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to support@studysoup.com