{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04_Functional

# 04_Functional - Functional Programming Pure Functional...

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

1 Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any). Evaluation does not produce side effects. The value of an expression cannot change over time. No notion of state . Computation may generate new values, but not change existing ones. Advantages • Simplicity No explicit manipulation of memory. Values are independent of underlying machine with assignments and storage allocation. Garbage collection. • Power – Recursion Functions as first class values Can be value of expression, passed as argument, placed in data structures. Need not be named.

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

View Full Document
2 Scheme (A Dialog of LISP) Primarily an interpreted language. Interacting with the interpreter Supply an expression to be evaluated Bind a name to a value (could be a function) > 3.14159 ; a number evaluates to itself 3.14159 > (define pi 3.14159) ; bind name to value pi > pi 3.14159 Expressions Prefix notation (op operand1 operand2 operand3 … ) General Expression Evaluation ( E 1 E 2 E 3 … E K ) Each E i is evaluated (in unspecified order) E 1 must evaluate to a function which is applied to the values of E 2 … E K Innermost (call-by-value) evaluation > (* 5 7) ; 35 > (+ 4 (* 5 7)) ; 39 Expressions (2) Uniform syntax is useful for manipulating programs as data. Some “special forms” are evaluated differently.
3 Function Definitions Lambda expressions (a special form) evaluate to anonymous functions: (lambda ( formal-parameters ) expression ) > (define square (lambda (x) (* x x))) ; square > (square 5) ; 25 > ( (lambda (x) (* x x)) 5) ; 25 Quoting Quote (a special form) prevents evaluation of its operand: > (define pi 3.14159) ; pi > pi ; 3.14159 > (quote pi) ; pi > ‘pi ; pi > (define f *) ; f > (f 2 3) ; 6 > (define f ‘*) ; f > (f 2 3) ; Error * not a procedure Lists A list is a sequence of zero or more values (symbols, numbers, booleans, functions, other lists).

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}