Functional programming: LISP • Originally developed for symbolic computing • First interactive, interpreted language • Dynamic typing: values have types, variables do not • Garbage-collected • Homogeneity of program and data • Extensible: multiple dialects, implementations. Convenient testbed for language design experiments • Main descendants: Scheme (compact) Common Lisp (massive). Statically typed functional languages: ML
Functional programming: LISP. Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables do not Garbage-collected Homogeneity of program and data - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Functional programming: LISP
• Originally developed for symbolic computing• First interactive, interpreted language• Dynamic typing: values have types, variables do not• Garbage-collected • Homogeneity of program and data• Extensible: multiple dialects, implementations.
Convenient testbed for language design experiments• Main descendants: Scheme (compact) Common Lisp
(massive). Statically typed functional languages: ML
Uniform syntax: lists
• Expressions are either atoms or lists• atoms are numeric or symbols• lists nest, to form full trees• Syntax is simple because programmer supplies what
would otherwise be the internal representation of a program:
(+ ( * 10, 12) (* 7, 11)) evaluates (10*12 + 7*11)• A program is a list:
(define (factorial n) (if (eq n 0) 1
( * n (factorial (- n 1))))
List manipulation
• Three primitives and one constant– get head of list: car– get rest of list: cdr– add an element to a list: cons– null list: nil or ()
• Add equality ( = or eq) and recursion, and this is a universal model of computation
Rule of evaluation
• A number evaluates to itself• An atom evaluates to its current binding• A list is a computation.
– Its first element must evaluate to an operation– the remaining elements are actual parameters– the result is the application of the operation to the
evaluated actuals
Quoted data
• If every list is a computation, how do we describe data?
• Another primitive: quote
(quote (1 2 3 4))
=> (1 2 3 4)
(quote (this is a simple declarative sentence)
=> (this is a simple declarative sentence)
‘ (this also works)
=> (this also works)
Decomposing a list
(car ‘(this is a list of symbols))
=> this
(cdr ‘(this is a list of symbols))
=> (is a list of symbols)
(cdr ‘(this that))
=> (that) ; a list (cdr ‘(singleton))
=> () ; the empty list
(car ‘()) ; run time error
Building lists
(cons ‘this ‘(that and the other))
=> (this that and the other)
(cons ‘a ‘())
=> (a)
useful shortcut: list
(list ‘a ‘b ‘c ‘d ‘e)
=> (a b c d e)
equivalent to
(cons ‘a (cons ‘b (cons ‘c (cons ‘d (cons ‘e ‘())))))
Control structures
• Conditional
(if condition expr1 expr2)• Generalized form
(cond
(pred1 expr1)
(pred2 expr2)
…
(else exprn)
Needs special rule: evaluate only the successful entry
if and cond are not regular functions
Function declarations
(define (sqr n) (* n n))• define is also special: body is not evaluated• defines produces a binding: sqr is bound to the body
of the computation:
(lambda (n) (* n n))• define can produce value bindings as well:
(define x 15)
(sqr x)
=> 225
Recursion
(define (add1 x) (+ x 1)) ; the beginnings of Peano arithmetic
(define (sub1 x) (- x 1))
(define (add x y)
(if (= y 0) x
(add ( (add1 x) (sub1 y))))
(define (times x y)
(cond
((= y 0) 0)
((= y 1) x)
(else (add x (times (x (sub1 y)))))))
Recursion (ii)
(define (exp x y)
(cond
((eq y 0) 1)
(else (times x (exp x (sub1 y))))))
better:
(define (fast-exp x y)
(cond (= y 0) 1)
( (even? y) (square (fast-exp x (/ y 2))))
(else (* x (fast-exp x (- y 1))))))
(define (even? n) (= (remainder n 2) 0)) ; defining a predicate
Recursion on lists
(define (member elmt lis)
(cond
((null? lis) ‘())
((eq elmt (car lis)) lis)
(else (member elmt (cdr lis)))))
convention: return rest of list, starting from elmt, rather than #t or #f
convention: every non-false value is true in a boolean context
Predicates
• If variables are untyped, need run-time tests to determine kind:
symbol?
number?
list?
null?
zero?
Syntax conventions differ in different dialects: symbolp, numberp, listp, zerop...
Functional arguments
(define (map fun lis)
(cond
((null? lis) ‘())
(cons (fun (car lis)) (map fun (cdr lis)))))
(map sqr (map sqr ‘( 1 2 3 4))
=> (1 16 81 256)
Self-definition
(define (eval exp env) ; the lisp interpreter
(cond
((number? exp) exp) ; numbers are self-evaluating
((symbol? exp) (lookup exp env)) ; a symbol has a binding