Functional Programming - Lisp 2
Functional Programming - Lisp 2
Functional
Programming
Languages
ISBN 0-321-49362-1
Introduction
loop
read in an expression from the console;
evaluate the expression;
print the result of evaluation to the console;
end loop.
1-9
Example
• LISP expressions are composed of forms. For example, cos(0) is
written as (cos 0).
• LISP expressions are case-insensitive. It makes no difference
whether we type (cos 0) or (COS 0).
• "+" is the name of the addition function that returns the sum of its
arguments.
• Numeric values like 4 and 6 are called self-evaluating forms: they
evaluate to themselves. To evaluate (+ 4 6) in applicative order,
the forms 4 and 6 are respectively evaluated to the values 4 and 6
before they are passed as arguments to the + function.
1-10
Numeric Functions
1-11
Define functions
1-12
Programming in Lisp
1-13
Control Structures
• n! = 1 if n = 1
• n! = n * (n - 1)! if n > 1
1-14
Relation Operators
1-15
Multiple Recursions
1-16
Lists
1-17
Quote
• QUOTE - takes one parameter;
returns the parameter without
evaluation
– QUOTE is required because
the Scheme interpreter,
named EVAL, always
evaluates parameters to
function applications before
applying the function.
QUOTE is used to avoid
parameter evaluation when
it is not appropriate
1-18
Basic List Functions
• Examples:
(CAR ′((A B) C D)) returns (A B)
(CAR ′A) is an error
(CDR ′((A B) C D)) returns (C D)
(CDR ′A) is an error
(CDR ′(A)) returns ()
(CONS ′() ′(A B)) returns (() A B)
(CONS ′(A B) ′(C D)) returns ((A B) C D)
(CONS ′A ′B) returns (A . B) (a dotted pair)
Dotted pair notation is a general syntax for cons cells that represents the CAR and CDR
explicitly. In this syntax, ( a . b ) stands for a cons cell whose CAR is the object a and
whose CDR is the object b . Dotted pair notation is more general than list syntax
because the CDR does not have to be a list.
1-25
Example Scheme Function:
member
• LISP defines a function (member E L) that returns non-NIL if E is a member of L.
1-26
Example Scheme Function:
append
• LISP defines a function append that appends one list by another:
1-27
Tail Recursion
1-28
Functions as First-Class Objects
1-29
Functions as First-Class Objects
1-31
Functions Returning Multiple Values
(defun order (a b)
"Return two values: (min a b) and (max a
b)." (if (>= a b)
(values b a)
(values a b)))
• Ruby Blocks
– Are effectively subprograms that are sent to
methods, which makes the method a higher-
order subprogram
– A block can be converted to a subprogram
object with lambda
times = lambda {|a, b| a * b}
Use: x = times.(3, 4) (sets x to 12)
– Times can be curried with
times5 = times.curry.(5)
Use: x5 = times5.(3) (sets x5 to 15)
Copyright © 2015 Pearson. All rights reserved. 1-35
Comparing Functional and Imperative
Languages
• Imperative Languages:
– Efficient execution
– Complex semantics
– Complex syntax
– Concurrency is programmer designed
• Functional Languages:
– Simple semantics
– Simple syntax
– Less efficient execution
– Programs can automatically be made
concurrent