scheme stuff

# scheme stuff - More Scheme Stuff-closures and tail...

• Notes
• 2

This preview shows pages 1–2. Sign up to view the full content.

More Scheme Stuff -closures and tail calling -loops in scheme are done through recursion -... actually has a specific meaning (do ((var1 intial1 step1) ...) (endtest expr...) body1 body2 recursive version: (let ((f (lambda (var1...) (if end-test (begin expr...) (begin body1 body2 ... (f s1 ...)))) )) (f i1 ...) -a closure is like an object with one method -(define compose (lambda (f g) (lambda (a) (f (g a))) )) -(define negative-inverse (compose - /)) -“-” makes the number negative -“/” makes a number its inverse - (define curry (lambda (f) (lambda (a) (lambda (b) (f a b) (define plus (curry +)) (define plus5 (plus 5)) (define nine (plus5 4)) -changing the order of arguments (define twist (lambda (f) (lambda (a b) (f b a)) )) (define subtract-from (twist -)) (subtract-from 9 11) ; yields 2

This preview has intentionally blurred sections. Sign up to view the full version.

(define minus1 ((curry subtract-from) 1))
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: (minus1 9) ; yields 8-functional programming is very powerful-it is harder to understand line by line, but there is much more meaning line by line -(reduce f (list a b c d)) gives (f a (f b (f c d)))) (reduce + (list 1 2 3 4 5)) ; yields 15 (zip * (list 1 2 3) (list 4 5 6)) is the same as (list (* 1 4) (* 2 5) (* 3 6))-define dot-product (lambda (u v)) (reduce + (zipper * u v)) ))-(define eval-line (lambda (x) (lambda (b a) (+ b (* a x)) ))) (define eval-poly (lambda (x) (lambda (l) (reduce (eval-line x) 1)))) ((eval-poly 2) (list 5 4 3 2 1)) ; yields 57-eval-poly 2 claims that any polynomial can be evaluated at the point 2 -reduce (lambda (b a) a*x+b) ' (5 4 3 2 1))-reduce is a combining operator -map puts stuff onto lots of stuff...
View Full Document

• Spring '14
• StevenWatt

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern