CS 36 – Chapter 11 Functional programming Features Practice Here’s a Scheme interpreter I found… http://sourceforge.net/projects/sisc Chapter 11? We’re in bankruptcy!
Functional programming Function calls dominate, not merely present Based on mathematical syntax, not efficient code Usually interpreted, not compiled Usually typeless… we manipulate numbers and string lists: “symbolic computation” Languages Lisp, Scheme, ML, Haskell
Comparison Imperative Functional Assignment statements Memory locations State of a machine Functional Expression evaluation Function calls Less attention to rep’n, implementation
The culture Working with functional language: interpreter. Loves to evaluate things interactive, immediate results like using adding machine, cash register, calculator Example… If you say “8 – 3”, you get answer. What would Java compiler tell you? But, what was so good about imperative languages: compiling (translating), wait & enter 2nd command to run?
Source code A functional “program” is a sequence of expressions constant variable (to be evaluated immediately) list (just to display like a constant) function call Use the same syntax for lists and function calls (turkey ham tuna) (- 8 3) Recursion instead of loops
Samples Prefix expressions (+ (* 3 4) 5 1) returns 18 define: variables for later use (define a 12) (define b 5) (+ a b) returns 17 lambda: Creating a function (lambda (x) (* 2 x)) Use both “define” and “lambda” to define function with name: (define double ) (double 55) returns 110
Functions Many functions in Scheme/Lisp need to be recursive. Factorial? (define fact (lambda (x) (* x (fact (- x 1)))) )
Conditionals Use cond to implement if-else (cond (expr value) ... (#t value)) Return value corresponding to first true expression we encounter. (cond ((< 5 0) -1) (= 5 0) 0) (#t 1))
Using cond (define sign (lambda (x) (cond ((< x 0) -1) (= x 0) 0) (#t 1) ) (sign 400) returns 1 (sign -333) returns -1 (sign 0) returns 0 Now we should be able to write a factorial function
Function on list Recall the built-in functions car and cdr. “caddr” is abbreviation of (car (cdr (cdr x))) (define third (lambda (x) (caddr x)) ) (third '(sugar cereal pasta asparagus treacle)) returns pasta Note that we need a quote for a list-constant or an atom from a list. x means evaluate the variable x. 'x means don’t evaluate x.
Defining a list (define list1 '(1 2 3)) (define list2 '((a b) (e f) (x y))) list1 returns (1 2 3) list2 returns ((a b) (e f) (x y)) Use cons to add one element to a list, or append for combining two lists. (cons 8 list1) does the same as (append ‘(8) list1) For review, what are: (car list2) (caar list2) (cadar list2) Appending to list2: (append ‘((g h)) list2)
Multiple variables Add 2 numbers Add a list of numbers (define sum (lambda (x y) (+ x y)) ) (sum 8 4) returns 12 Add a list of numbers (define sumlist (lambda (x) (+ (car x) (sumlist (cdr x)))) Why is this wrong?
Better sumlist When working with lists, it’s often helpful to use comparisons such as: (null? list) to see if a list is empty (eq? x y) to see if two atoms are equal (define sumlist (lambda (x) (cond ((null? x) 0) (#t (+ (car x) (sumlist (cdr x)))) ) (sumlist '(5 4 7 3 2 6 1 8 10 9)) returns 55
Creating functions in Scheme/Lisp List manipulation Numerical examples Database application Property list Association list
Review Built-in functions Cambridge Polish notation: (f x), not f(x) car, cdr, cons cond define, lambda null?, eq? (not to confuse with =) display Cambridge Polish notation: (f x), not f(x) Recursion replaces iteration Elegant, compact, focus only on evaluation
Examples Let’s begin with finding the length of a list. (define length (lambda (L) (cond ((null? L) 0) (#t (+ 1 (length (cdr L)))) ) (length '(Klaatu barada nikto)) returns 3 Next: we can write a similar function to count how many times a certain word appears in a list.
Further examples Basic list manipulation (number_matches word L) (get_word L n) (get_last_word L) (replace_first L word) (replace_cp) (replace_word) (reverse L) Number lists (abs L) ; more in lab “Database” functions (get_age R) (set_age R new_age) (get_salary R) (set_salary R new_salary)