Lecture overview -- Keyboard shortcut: 'u'  Previous page: Lambda calculus -- Keyboard shortcut: 'p'  Next page: Function objects -- Keyboard shortcut: 'n'  Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home    Lecture 2 - Page 35 : 46
Functional Programming in Scheme
Expressions, Types, and Functions
Functions in Scheme

Functions are represented as lambda expressions in a source program

At run time, functions are represented as first class function objects

> (define x 6)

> (lambda (x) (+ x 1))
#<procedure>

> (define inc (lambda (x) (+ x 1)))

> inc
#<procedure:inc>

> (if (even? x) inc fac)
#<procedure:inc>

> ((if (even? x) inc fac) 5)
6

A sample read-eval-print session with lambda expressions and function objects. In a context where we define x to the number 6 we first evaluate a lambda expression. Scheme acknowledges this by returning the function object, which prints like ' #<procedure> '. As a contrast to numbers, lists, and other simple values, there is no good surface representation of function values (function objects). Next we bind the name inc to the same function object. More about name binding in a later part of this material. The expression (if (even? x) inc fac) returns inc because the value of x is 6, and as such it is even. Therefore the value of ((if (even? x) inc fac) 5) is the same as the value of (inc 5), namely 6.