COP 4020 meeting -*- Outline -*- * Example: learning Scheme ** motivation ------------------------------------------ MOTIVATIONS FOR FUNCTIONAL PROGRAMMING 1. Parallelism is easier - fewer dependencies - programs have simple dependencies - analysis can isolate effects (interesting type systems) 2. Modularity can be more thorough - all patterns can be abstracted 3. Ideas from functional programming are used in more widely-used languages - Java and C# both have anonymous functions (lambdas) - Scala blends OO and functional ideas 4. Interesting features for recursion - pattern data - cases based on patterns - tail recursion optimization ------------------------------------------ ** programming model ------------------------------------------ PROGRAMMING MODEL Model: programs are modeled as Programs represented as ------------------------------------------ ... functions and compositions of functions (over lists) ... data (lists) ** Basic elements of a language *** Data (means of computation) ------------------------------------------ WHAT DATA? For Scheme/Lisp programming use: data syntax examples - literals - symbols ------------------------------------------ ... 3, 3.14 ... 'tree, 'foo ** means of combination *** compound data traditionally called s-expressions in Lisp these are lists (and others) ------------------------------------------ COMPOUND DATA Lists: (1 2 3 4) (sentence (np (n ron)) (vp (v gave) (np (art a) (n paper)) (compls (prep to) (np (n sue))))) Functions: (lambda (x) x) (lambda (x y) (cons x (cons y 'nil))) (lambda (f) (lambda (x) (lambda (y) (f x y)))) ------------------------------------------ Note the recursive structure of lists ::= ( * ) which is represented as: ::= nil | ( T . ) Note: a list such as (1 . (2 . (3 . nil))) is printed as (1 2 3) (cons X Y) is made into a pair (X . Y) and if Y is a list then so is (cons X Y) Note: there are also other kinds of compound data in Scheme, such as vectors *** compound expressions ------------------------------------------ COMPOUND EXPRESSIONS Arithmetic operators: (+ 3 4) (* 3 (- 8 2)) (> 0 i) Building lists: (cons 3 (cons 4 'nil)) (list 5 7 8 x) Building compound tests: (and (>= 0 i) (< i (size lst))) (or (study? u) (miracle? u course)) Conditional evaluation: (if (not (zero? n)) (/ total n) 0) ------------------------------------------ In Scheme/Lisp the operator is first in a call ** means of abstraction (usually naming) *** variable definitions ------------------------------------------ NAMING DATA (define total 0) (define data (read)) (define average (lambda (x y) (/ (+ x y) 2))) ------------------------------------------ Q: In the last example, what is the data being named? a function (closure) Q: In the last example, what is being named? both average and x and y **** scope of variables The scope of a variable is the area of the program where its definition is available. ------------------------------------------ SCOPE OF VARIABLES (define average (lambda (x y) (/ (+ x y) 2))) (define greet (lambda (x y) (write "Greetings, ") (write x) (write " ") (write y) (newline))) ------------------------------------------ Q: Does the x in average have anything to do with the x in greet? no The scope is limited by the parentheses *** Examples **** data example ------------------------------------------ AN EXAMPLE OF SCHEME DATA (define story '((A thirsty crow saw a pitcher) (The crow flew to the pitcher) (But the water was too low to reach) (He tried to break it but could not) (Seeing some small pebbles (he dropped many of them (into the pitcher))) (This raised the water to the brim) (The crow drank the water))) ------------------------------------------ **** primitive functions on data ------------------------------------------ (define is-story-empty (null? story)) (define first-sentence (car (car story))) (define first-word (car first-sentence)) ;;; return 1 if the arguments are the same ;;; and 0 if they differ (define same? (lambda (word1 word2) (if (equal? word1 word2) 1 0))) ------------------------------------------ **** defining functions on data ------------------------------------------ (define rest (lambda (lst) (cdr lst))) (define length (lambda (lst) (if (null? lst) 0 (+ 1 (length (rest story)))))) (define count (lambda (word lst) (count-iter word lst 0))) (define count-iter (lambda (word lst count) (if (null? lst) count (count-iter word (cdr lst) (+ (same? word (car lst)) count))))) ------------------------------------------ These are recursive. That works well because the data is recursive The iteration is a pattern.