Lisp - Lisp Functions, recursion and lists cs3723 1 The...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
cs3723 1 Lisp Functions, recursion and lists
Background image of page 1

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

View Full DocumentRight Arrow Icon
cs3723 2 The Lisp Programming Language Stems from interest in symbolic computation Math, logic, artificial intelligence Functional programming paradigm A program is a expression Expresses flow of data; map input values to output values No concept of control-flow or statements No side effects or modification to variables Functions are first-class objects A function can be used everywhere a regular value 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 prototype systems incrementally X Not many tools or libraries; Slow in speed/efficiency (interpreted)
Background image of page 2
cs3723 3 Concepts in Lisp Supported value types Atomic values: numbers (e.g. 3, 7.7), symbols (e.g. ‘abc), booleans Compound data structures: lists (car, cons, cdr), functions (lambda) Supported operations Function definition and function call (define fname (lambda (parameters) body)) (fname arguments) Predefined functions: cons, cond, if, car, cdr, eq?, …… Innovations in language design Functional programming paradigm A program is composed of expressions Functions are first-class objects Higher-order functions Abstract view of memory (the Lisp abstract machine) Program as data (dynamic interpretation of program)
Background image of page 3

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

View Full DocumentRight Arrow Icon
cs3723 4 Interacting with Scheme (define pi 3.14159) ; bind pi to 3.14159 (lambda (x) (* x x)) ; anonymous function (define sq (lambda (x) (* x x))) (define (sq x) (* x x)) ; (define sq (lambda (x) (* x x))) (sq 100) ; 100 * 100 (if P E1 E2) ; if P then E1 else E2 (cond (P1 E1) (P2 E2) (else E3)) ; (if P1 E1 (if P2 E2 E3)) (let ((x1 E1) (x2 E2)) E3) ; declare local variables x1 and x2 ((lambda (x1 x2) E3)) E1 E2) (let* ((x1 E2) (x2 E2)) E3) ; E2 can use x1 as a local variable (lambda (x1) ((lambda (x2) E3) E2) E1)
Background image of page 4
5 Expressions, Statements and Declarations Expression (x+5)/2 Syntactic entity that has a value Need not change accessible memory If it does, has a side effect Statement load 4094 r1 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 declaration does
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 20

Lisp - Lisp Functions, recursion and lists cs3723 1 The...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online