Episode 6: Working with Recursion

9 downloads 1841 Views 74KB Size Report
Page 2 ... (sub1 n)))])). (how-i-love-thee 5) =>. (list 5 4 3 2 1 "let me count the ways") .... you for your messages. Sophia: I think you are a hopeless romantic.
Episode 6: Working with Recursion

(You have 3 unread messages)

(You have 3 unread messages) Last week, the internet went down for a whole 5 hours at UW.

(You have 3 unread messages) Last week, the internet went down for a whole 5 hours at UW. That got me thinking.

(You have 3 unread messages) Last week, the internet went down for a whole 5 hours at UW. That got me thinking. Which is why I wrote this.

(You have 3 unread messages) Last week, the internet went down for a whole 5 hours at UW. That got me thinking. Which is why I wrote this. I know that you would be fine without the internet, since you know so much Scheme.

(You have 3 unread messages) Last week, the internet went down for a whole 5 hours at UW. That got me thinking. Which is why I wrote this. I know that you would be fine without the internet, since you know so much Scheme. While the internet was down, I wrote a program.

(define (how-i-love-thee n) (cond [(= n 0) (list "let me count the ways")] [else (cons n (how-i-love-thee (sub1 n)))]))

(define (how-i-love-thee n) (cond [(= n 0) (list "let me count the ways")] [else (cons n (how-i-love-thee (sub1 n)))])) (how-i-love-thee 5) =>

(define (how-i-love-thee n) (cond [(= n 0) (list "let me count the ways")] [else (cons n (how-i-love-thee (sub1 n)))])) (how-i-love-thee 5) => (list 5 4 3 2 1 "let me count the ways")

(define (how-i-love-thee n) (cond [(= n 0) (list "let me count the ways")] [else (cons n (how-i-love-thee (sub1 n)))])) (how-i-love-thee 5) => (list 5 4 3 2 1 "let me count the ways") – Michael

(You have 2 unread messages)

(You have 2 unread messages) Wait.

(You have 2 unread messages) Wait. Actually, I wanted to put the string first then countup.

(You have 2 unread messages) Wait. Actually, I wanted to put the string first then countup. Like: (list "let me count the ways" 1 2 3 4 5)

(define (count-up k n)

(define (count-up k n) (cond

(define (count-up k n) (cond [(= k n)

(define (count-up k n) (cond [(= k n) (cons n empty)]

(define (count-up k n) (cond [(= k n) (cons n empty)] [else

(define (count-up k n) (cond [(= k n) (cons n empty)] [else (cons

(define (count-up k n) (cond [(= k n) (cons n empty)] [else (cons k

(define (count-up k n) (cond [(= k n) (cons n empty)] [else (cons k (count-up

(define (count-up k n) (cond [(= k n) (cons n empty)] [else (cons k (count-up (add1 k) n))])

(define (count-up k n) (cond [(= k n) (cons n empty)] [else (cons k (count-up (add1 k) n))]) But, I need to say something first...

(define (how-i-love-thee-too n) (cons

(define (how-i-love-thee-too n) (cons "let me count the ways"

(define (how-i-love-thee-too n) (cons "let me count the ways" (count-up

(define (how-i-love-thee-too n) (cons "let me count the ways" (count-up 1 n)))

(define (how-i-love-thee-too n) (cons "let me count the ways" (count-up 1 n))) (how-i-love-thee-too 3) =>

(define (how-i-love-thee-too n) (cons "let me count the ways" (count-up 1 n))) (how-i-love-thee-too 3) => (list "let me count the ways" 1 2 3) – Michael

(You have 1 unread message)

(You have 1 unread message) One more thing:

(You have 1 unread message) One more thing: Here is an association list of my friends in case you want to find out that I am not that bad a guy.

(You have 1 unread message) One more thing: Here is an association list of my friends in case you want to find out that I am not that bad a guy. (define friends (list (list 5551739 "steve") (list 5552144 "oliver") (list 5553322 "allan")))

– Michael

(You have one new text message)

(You have one new text message) Sophia: Thank you for your messages.

(You have one new text message) Sophia: Thank you for your messages. Sophia: I think you are a hopeless romantic.

(You have one new text message) Sophia: Thank you for your messages. Sophia: I think you are a hopeless romantic. Sophia: So, you probably won’t mind me saying...

(You have one new text message) Sophia: Thank you for your messages. Sophia: I think you are a hopeless romantic. Sophia: So, you probably won’t mind me saying... Sophia: You had me at (

End of Episode 6