t03E_functional5 - Principles of Principles Programming Languages Programming Topic Functional Programming V Professor Lou Steinberg Fall 2013 CS 314

t03E_functional5 - Principles of Principles Programming...

This preview shows page 1 - 9 out of 35 pages.

CS 314, Steinberg: L09 Functional Programming V F13 1 Principles of Principles of Programming Languages Programming Languages Topic: Functional Programming V Professor Lou Steinberg Fall 2013
Image of page 1
CS 314, Steinberg: L09 Functional Programming V F13 2 Midterm exam 1 Thursday, Oct 17 Regular lecture time Regular lecture room Practice exams are on Sakai, topic list coming This Tuesday, Oct 15; review email me questions or topics
Image of page 2
CS 314, Steinberg: L09 Functional Programming V F13 3 Warning for Project 1 Warning for Project 1 Consider (def i ne (eval-order n) (if (= n 0) '( ) (cons (begin (display n) n) (eval-order (- n 1))))) In what order are the args to cons evaluated?
Image of page 3
CS 314, Steinberg: L09 Functional Programming V F13 4 Warning for Project 1 Warning for Project 1 In what order are the args to cons evaluated? Can't affect a pure functional program But I/O is a side-effect, not functional Beware of evaluation-order in last-n Affects display-window See eval-order.scm, compare Racket and MIT/GNU Schemes
Image of page 4
CS 314, Steinberg: L09 Functional Programming V F13 5 Where are we? Where are we? Functional programming Binding in place of assignment Recursion in place of iteration functions as f i rst class data type functions that take functions as arguments functions that return functions lexical binding and closures functions inside of data structure [Scheme macros] lazy evaluation
Image of page 5
CS 314, Steinberg: L09 Functional Programming V F13 6 Review: Functions as Results Review: Functions as Results If a function is a piece of data we can return it from a function as a result eg add-check in project 1 Especially useful with lexical binding
Image of page 6
CS 314, Steinberg: L09 Functional Programming V F13 7 Lexical Binding Lexical Binding How would you write map? (def i ne (map fn lst) (if (null? lst) ‘( ) (cons (fn (car lst)) (map fn (cdr lst))))) But what if we do (let ((lst ‘(a b)) (foo ‘(x y z))) (map (lambda (w)(cons w lst)) foo))
Image of page 7
CS 314, Steinberg: L09 Functional Programming V F13 8 Lexical Binding Lexical Binding Scope is based on grammatical structure E.g. binding of a let is visible in the body of the let, unless shadowed As opposed to dynamic binding, where the binding of the let would be visible from entry to exit unless shadowed The distinction only matters if you can go back to a scope after you have left it
Image of page 8
Image of page 9

You've reached the end of your free preview.

Want to read all 35 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes