











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Stems from interest in symbolic computation math, logic, artificial intelligence Functional programming paradigm A program/function is a expression Expresses flow of data in evaluations ==> map input values to output values No concept of control-flow or statements (the ordering of expressions does not matter) Functions are first-class objects A function can be used everywhere a regular value (e.g., an integer) is used Functions can take other functions as parameters and return other functions as results (higher-order functions) Adding side-effect operations Different occurrences of expressions have different values Strength and weakness Simplicity and flexibility Build systems incrementally as the results of experiments X Not many tools or libraries; Slow in speed/efficiency (interpreted)
Expression (x+5)/ Syntactic entity that has a value Need not change accessible memory If it does, has a side effect Statement load 4094 r Imperative command Alters the contents of previously-accessible memory Declaration integer x Introduces new identifiers (variables) May bind value to identifier, specify type, etc. Scoping rules for variables: which variable does each name refers to?
(define pi 3.14159) ; bind pi to 3. (lambda (x) (* x x)) ; anonymous function (define (sq x) (* x x)) ; (define sq (lambda (x) (* x x))) (sq 100) ; 100 * 100 (if P E1 E2) ; if P then E1 else E (cond (P1 E1) (P2 E2) (else E3)) ; (if P1 E1 (if P2 E2 E3)) (let ((x1 E1) (x2 E2)) E3) ; ((lambda (x1 x2) E3) E1 E2) ; ((lambda (x1) (lambda (x2) E3)) E1) E2) (let* ((x1 E2) (x2 E2)) E3) ; (lambda (x1) ((lambda (x2) E3) E2) E1) Programming environment DrScheme available on Windows/Linux machines Documents available from class web site
‘(a b c) ‘(+ 2 (* 3 5)) ‘(lambda (a b) (cons a b)) A dynamically typed list can be used to implement most pointer-based data structures lists, trees, graphs, etc.
Pure Lisp Expressions do not modify observable machine states No side effects Impure Lisp Allow modifications to memory May increase efficiency of programs Eg. Modify a single element in a list (set! x y) Replace the value of x with y (rplacea ’(A B) y) or (set-car! ’(A B) y) replace A (the address field of ’(A B)) with y (rplaced ’(A B) y) or (set-cdr! ’(A B) y) replace B (the decrement field of ’(A B)) with y Sequence operator (progn (set! x y) x) or (begin (set! x y) x) Set the value of x to be y; then returns the value of x
Programming steps What are the input parameters? What values could each parameter take? Enumerate each combination of input parameters, give a return value for each case Exercise problems Define a function Find which takes two parameters, x and y. It returns x if x appears in y, and returns an empty list (‘()) otherwise. Define a function substitute which takes three parameters, x, y, and z. It returns a new list which replaces all occurrences x in y with z. Define a function Max which takes a single parameter x, and returns the maximal number that appears in x.
Lisp program: exp ::= value | variable | (exp exp exp…) Can be represented using Lisp atoms and lists Can be built at runtime and then evaluated An eval function used to evaluate contents of list in Scheme, need to choose a more advanced language level (define atom? (lambda (x) (or (symbol? x) (number? x) (boolean? x)))) (define substitute (lambda (x y z) (cond ((null? z) z) ((atom? z) (if (eq? z x) y z)) (else (cons (substitute x y (car z)) (substitute x y (cdr z))))))) (define substitute-and-eval (lambda (x y z) (eval (substitute x y z)))) (substitute-and-eval ’x ’3 '(+ x 1))
(lambda (f g x) (f (g x)))
int compose(int (f)(...), int (g)(...), int x) { return f(g(x)); }
(define maplist (f x) (cond ((null? x) nil) (else (cons (f (car x)) (maplist f (cdr x))))))
Cell* maplist(int (f)(...), Cell x) { if (x == NULL) return NULL; else { Cell* res = (Cell*) malloc (sizeof(Cell)); res->val=f(x->val); res->next=maplist(f,x->next); return res; } }
(lambda (f g) (lambda (x) (f (g x))))))
int compose(int (f)(...), int (g)(...), int x) { return f(g(x)); } In Scheme The function compose takes only two parameters The result of compose is another function in C The function compose takes three parameters The result of compose if a concrete value C does not allow functions being returned as result, why?
Implementing Lisp --- The Memory Model Cons cells Atoms and lists represented by cells Address Decrement Atom A Atom B Atom C
(a) (b) Both structures could be printed as (A.B).(A.B) Which are the results of evaluating (cons (cons ‘A ‘B) (cons ‘A ‘B))? ((lambda (x) (cons x x)) (cons ‘A ‘B)) Equality of compound structures What is the result of (eq? ‘a ‘a)? What is the result of (eq? ‘(a b) ‘(a b))?
Garbage Memory locations in the heap that are no longer accessible Examples (car (cons ( e 1 ) ( e 2 ) )) Cells created in evaluation of e 2 may be garbage, unless shared by e 1 or other parts of program ((lambda (x) (car (cons (… x…) (... x ...))) '(Big Mess)) The car and cdr of this cons cell may point to overlapping structures.