Sei sulla pagina 1di 73

Functional

Programming

Functional programming
The primary method of computation is the
application of functions to arguments

Immutable & stateless


Allows less error, cleaner, and elegant code

Problem domain

Academic Discussion
Mathematical Evaluation
Software Simulation Training (AI)
Financial Computation
Web Development
Hardware Design

History and evolution


at the very foundation, the basis of most
functional programming language is

Lambda Calculus
a formal system in mathematical logic
(especially in computational)
theoretical framework for describing
functions & their evaluation

Existing languages
Lisp

early functional language. introduced several


features for functional-flavored programming
languages

Intermediate Programming Language(IPL)


first computer based functional programming language.
assembly-styled

FORTRAN
Hope
Rex

Important
concepts
In functional programming

First class and higher order functions


FC: ComSci
HO: Math
Functions that take in functions as arguments

Differential operator

First class and higher order functions


Application
Partial Application and Currying:
applying arguments one at a time, returning a new
function that takes the next argument

a + b * c a.add(b.multiply(c))

Recursion
Iteration is often expressed
as recursion
Tail Recursion:
the function is called at the end of the function
often optimized in Functional Programming during
compilation

Pure functions
Functions that only returns a value
do not modify any state, like changing the
value of a global variable
this restricted action is called a side effect

Pure functions
Allows For Optimization
removal
run in parallel (threading)
function reordering
memoization

evaluation
print length([2+1, 3*2, 1/0, 5-4])

Strict
Fail at 1/0

non-strict
4

WHY?

evaluation
print length([2+1, 3*2, 1/0, 5-4])

Strict
Evaluates everything

non-strict
Only evaluates when needed

LISP

LIST PROCESSING

LIST PROCESSING

LIST PROCESSING

second-oldest programming language that is still in widespread use


Next to

1956

FORTRAN
1958

1981

CONCEPTUALIZED
1956

1958

1981

John McCarthy

Massachusetts Institute of Technology (MIT)

1956

1958

1981

lisp

Initially designed for performing mathematical


notations
LAMBDA CALCUluS
Favored by researchers in Artificial Intelligence
for its capacitY and support for s y m b o l i c
programming

Alonzo Church

1956

1958
1958

1981

lisp

Initially designed for performing mathematical


notations
LAMBDA CALCUluS
Favored by researchers in Artificial Intelligence
for its capacitY & support for s y m b o l i c
programming

1956

1958
1958

1981

DIALECTS OF lisp
MacLisp

ZetaLisp

Scheme

Interlisp
1980s

1956

1958

1981

LISP MACHINES

Runs entirely on lisp


Need for power to run
complex aI software
Distributed by
companies with
their own versions
First single-user
workstations

1956

1958

1980s

1981

COMMON LISP

PROBLEM: large number of lisp dialects


May cause lisp community
Solution: standardize a new language
Have best features of lisp dialects

1956

1958

1981

PROGRAMMING PARADIGMS
Functional

Procedural

Reflective

Meta

BASIC STRUCTURE

LISP EXPRESSIONS
Symbolic expressions
3 valid objects:
ATOM
LIST
STRING

ATOM
yo
thisIsAnAtom

atomA

LIST
()
(yo)
(atom1 atom2)
(atom1 (atom2inlist) atom3)

STRING
yo
thisIsAString

Hallo world!

SPECIAL SYMBOLS
T
set of all objects (True, yes, 1)

nil
empty data type (False, no, 0)

INTERPRETED
&
COMPILED

Read-Evaluate-print-loop
REPL
Check source code in repeated loop
Reads symbolic expression, evaluates
it, and prints the result

PREFIX NOTATION
Operands come after the operators
(+ 2 3)
(+ 1 2 3)

NAMES
Identifiers are symbols (variables in
terms of form)
composed of alphanumeric characters,
underscores, and hyphens
this-asymbol_5
anothersymbol

TYPE-CHECKING

strong static type-checking

BINDING

DYNAMIC BINDING

SCOPING

Static scoping

LIFETIME
Dynamic variables in Lisp have indefinite
lifetimes stay until everything finishes
running
Variables bound with let only last until
the end of the defining form

Data Types
Set of objects
Each object belongs to at least one
data type
Explicit declaration of data type is
not necessary

FUNCTIONS ON Data Types


Typep
determines if a certain object belongs to a certain
set/data type which returns a value of t if it
belongs to the set and nil if not

Type-of
Returns data type of an object

Data types: numbers


Integer
fixnum - expressible within machine range (e.g. 1, -1, 500)
bignum - integers that do not fall under fixnum (e.g. 25!)

Ratio
fractional form of a/b expressed in code as (/ a b)

Floating point
decimal form (e.g. 4.589, -10.2, 3.00.)

Complex numbers

numbers with imaginary components a+bi is expressed as #C(a b)

Data types: boolean


T
True

Nil
false

Data types: characters


Standard characters
begin with #\ followed by any of the following characters:
!@#$%&*+()-,./<>?;:{}\~[]|^_=`
A.Z
a.z
0...9

Special characters
#\Backspace, #\Tab, #\linefeed

Data types: others


Arrays
Vectors

Hash tables
series of elements with key and value component

List
most important data type since lisp is heavy on list processing

Recall Data types


Strings
series of characters enclosed in quotation marks

symbols
composed of alphanumeric characters, underscores, and
hyphens

Control structures
Conditional

If

Progn is used to block multiple statements &


returns the value of the final statement

When
single variable variant of if
returns NIL when condition is not met

UNLESS
single variable variant of if
returns NIL when condition is met

case

LOOP

LOOP for

do

subprograms

subprograms

subprograms

STRENGTHS AND
WEAKNESSES

Strengths
Garbage Collection
Dynamic Typing
Support for macros
every programmer can make his own extension

Powerful Error Handling


signaling
handling
restarting

Strengths
Decent Performance
relative to C++, Lisp is slower by just 1-2 times
compared to Python which is 2-100 times slower.

Portable across hardware and OS

Weaknesses
Readability
namespace
parentheses
prefix notation

Weaknesses
Support for macros
Common Lisp
Scheme
Clojure

Dynamic Typing
strong vs weak typing

Comparative
critique

WITH SCHEME
Scheme more minimalistic
Common Lisp more functions
More numerical support on Lisp
Tail calls are optimized in Scheme, possible in
Common Lisp but not required

WITH SCHEME
Scheme has one namespace
Common Lisp has separate namespaces

for functions and objects (i.e.


you may have a function and
an object with the same name)

WITH python
Python is more dynamic than Lisp, so harder
to check for errors

Python has no macros, Lisp does have them


Common Lisp a lot faster (rel. to C++,

Common Lisp is 1-2 times slower and Python 2-100


times slower)

WITH python
Python

One namespace in
has distinctions for statements and
expressions; everything in Common Lisp is an expression

Python

Objects are prevalent in both languages

Both languages employ automatic garbage collection

CURRENT
IMPLEMENTATIONS

Current implementations
Several other implementations of Lisp exist apart from
Common, including:

1.
2.
3.
4.
5.
6.
7.

CLISP
CMUCL (main goal is ANSI compatibility)
ECL (Embeddable CL)
CCL (Clozure CL)
SBCL
ABCL (Armed Bear CL, runs on Java)
MKCL

Real life applications


Authorizers Assistant is a system
used by American Express for
authorizing transactions

Real life applications


SPIKE schedules the observation
periods of NASAs Hubble Space
Telescope

Real life applications


The visual language PWGL is used for
synthesizing sounds & aiding
musical composition

Real life applications


Mirai, a 3D animation suite, was used to animate the
face of Gollum

DEMO

Quiz

Potrebbero piacerti anche