Docsity

Lecture Slides on The Lisp - Programming Languages | CS 3723, Study notes of Programming Languages

Partial preview of the text

Download Lecture Slides on The Lisp - Programming Languages | CS 3723 and more Study notes Programming Languages in PDF only on Docsity!

Lisp

Functions, recursion and lists

The Lisp Programming Language

 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)

Expressions, Statements and

Declarations

 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?

Interacting with Scheme

(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

Lists in Scheme

 In Scheme(Lisp), a list may contain arbitrary kinds

of values

 ‘(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.

 Scheme lists can be used to naturally implement

ASTs (abstract syntax trees)

lambda +

list cons 2 *

a b a b 3 5

 What about graphs (can we build cycles in lists)?

Lisp: Adding Side Effects

 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

Exercises

Programming in Lisp(Scheme)

 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.

Meta-programming

Programs As Data

 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))

Functions as Parameters

 function composition

(lambda (f g x) (f (g x)))

vs.

int compose(int (f)(...), int (g)(...), int x) { return f(g(x)); }

 Apply a function to each element in a list

(define maplist (f x) (cond ((null? x) nil) (else (cons (f (car x)) (maplist f (cdr x))))))

vs.

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; } }

Return functions as results

 function composition

(define compose

(lambda (f g) (lambda (x) (f (g x))))))

vs.

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

Sharing

 (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))?

A B A B
A B

Detecting Garbage

 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.