Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Functional Programming

The document discusses the Functional Programming (FP) paradigm, highlighting its theoretical foundations, characteristics, and importance in software development. It contrasts FP with imperative programming, emphasizing the simplicity and abstraction offered by FP languages like Haskell and Lisp, which focus on mathematical functions and avoid side effects. Additionally, it covers key concepts such as lazy evaluation, higher-order functions, and the applications of FP in areas like artificial intelligence and rapid prototyping.

Uploaded by

tamilpandi469
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Functional Programming

The document discusses the Functional Programming (FP) paradigm, highlighting its theoretical foundations, characteristics, and importance in software development. It contrasts FP with imperative programming, emphasizing the simplicity and abstraction offered by FP languages like Haskell and Lisp, which focus on mathematical functions and avoid side effects. Additionally, it covers key concepts such as lazy evaluation, higher-order functions, and the applications of FP in areas like artificial intelligence and rapid prototyping.

Uploaded by

tamilpandi469
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

THEORY AND DESIGN OF PROGRAMMING LANGUAGES

Functional Programming
Functional Programming

• The Functional Programming Paradigm is


one of the major programming paradigms.
– FP is a type of declarative programming paradigm
– Also known as applicative programming and value-oriented
programming
• Idea: everything is a function
• Based on sound theoretical frameworks (e.g.,
the lambda calculus)
• Examples of FP languages
– First (and most popular) FP language: Lisp
– Other important FPs: ML, Haskell, Miranda, Scheme, Logo
Functional Programming Languages

The design of the imperative languages is based


directly on the von Neumann architecture
Efficiency is the primary concern, rather than the
suitability of the language for software development

The design of the functional languages is based


on mathematical functions
A solid theoretical basis that is also closer to the user,
but relatively unconcerned with the architecture of the
machines on which programs will run
Mathematical Functions

• Def: A mathematical function is a mapping of members of one


set, called the domain set, to another set, called the range set.
• The lambda calculus is a formal mathematical system devised
by Alonzo Church to investigate functions, function application
and recursion.
• A lambda expression specifies the parameter(s) and the
mapping of a function in the following form
x .x*x*x
for the function cube (x) = x * x * x
• Lambda expressions describe nameless functions
• Lambda expressions are applied to parameter(s) by placing the
parameter(s) after the expression
( x . x * x * x) 3 => 3*3*3 => 27
( x,y . (x-y)*(y-x)) (3,5) => (3-5)*(5-3) => -4
Characteristics of Pure FPLs

Pure FP languages tend to


– Have no side-effects
– Have no assignment statements
– Often have no variables!
– Be built on a small, concise framework
– Have a simple, uniform syntax
– Be implemented via interpreters rather than
compilers
– Be mathematically easier to handle
Importance of FP
• In their pure form FPLs dispense with the notion of
assignment
– it's easier to program in them
– easier to reason about programs written in them
• FPLs encourage thinking at higher levels of
abstraction
– support modifying and combining existing programs
– thus, FPLs encourage programmers to work in units larger than
statements of conventional languages: "programming in the large"
• FPLs provide a paradigm for parallel computing
– absence of assignment (or single assignment) } provide basis
– independence of evaluation order } for parallel
– ability to operate on entire data structures } functional
Importance of FP
• FPLs are valuable in developing executable
specifications and prototype implementations
– Simple underlying semantics
• rigorous mathematical foundations
• ability to operate on entire data structures
ideal vehicle for capturing specifications
• FPLs are very useful for AI and other
applications which require extensive symbol
manipulation.
• Functional Programming is tied to CS theory
– provides framework for viewing decidability questions
• (both programming and computers)
– Good introduction to Denotational Semantics
• functional in form
Expressions
• Key purpose of functional programming: to extend the advantages of
expressions (over statements) to an entire programming language
• Backus* has said that expressions and statements come from two
different worlds.
– expressions: (a + b) * c arithmetic
(a + b) = 0 relational
¬(a  b) boolean
– statements: the usual assortment with assignment singled out
– assignments alter the state of a computation (ordering is
important)
e.g. a:= a * i; i:= i + 1
• In contrast, ordering of expressions is not side-effecting and therefore
not order dependent
FPLs Principles of Structuring
1) Transparency of meaning
– Meaning of whole expression can be understood in terms of meanings of its
subexpressions.
2) Transparency of Purpose
– Purpose of each part consists solely of its contribution to the purpose of the whole.
=> No side effects.
3) Independence of Parts
– Meaning of independent parts can be understood completely independently.
– In E + F, E can be understood independently of F.
4) Recursive Application
– Both construction and analysis of structure (e.g. expressions) can be accomplished
through recursive application of uniform rules.
5) Narrow Interfaces
– Interface between parts is clear, narrow (minimal number of inputs and outputs)
and well controlled.
6) Manifestness of Structure
– Structural relationships among parts are obvious. e.g. one expression is
subexpression of another if the first is textually embedded in the second.
Expressions are unrelated if they are not structurally related.
Properties of Pure Expressions
• Value is independent of evaluation order
• Expressions can be evaluated in parallel
• Referential transparency
• No side-effects
• Inputs to an expression are obvious from written
form
• Effects of operation are obvious from written
form
=> Meet Hoare's principles well
=> Good attributes to extend to all
programming (?)
What are some FPLs?

• LISP – the first FPL, ~1958


• Pure FPLs have no side effects
– Haskell and Miranda are the two most popular
examples
• Some FPLs try to be more practical and
do allow some side effects
– Lisp and its dialects (e.g. Scheme)
– ML (Meta Language) and SML (Standard ML)
Some FP concepts

• A number of interesting programming


language concepts have arisen, including:
– Curried functions
– Type inferencing
– Polymorphism
– Higher-order functions
– Functional abstraction
– Lazy evaluation
Haskell
• Similar to ML (syntax, static scoped, strongly
typed, type inferencing)
• Different from ML and most other FPLs in that it
is purely functional -- no variables, no assignment
statements, and no side effects of any kind
• Some key features:
- Uses lazy evaluation (evaluate no
subexpression until the value is needed)
- Has “list comprehensions,” which allow it to
deal with infinite lists
• Example:
fib 0 = 1
fib 1 = 1
fib (n + 2) = fib (n + 1) + fib n
A simple Haskell

factorial :: Int -> Int


factorial 0 = 1
factorial n = n * factorial (n - 1)

main = do
putStrLn "Enter a non-negative integer:"
input <- getLine
let n = read input :: Int
putStrLn $ "The factorial of " ++ show n ++ " is " ++
show (factorial n)
Lazy
evaluation
• An evaluation strategy in which arguments to a
function are evaluated only when needed for the
computation.
• Supported by many FPLs including Scheme, Haskell
and Common Lisp.
• Very useful for dealing with very large or infinite
streams of data.
• Usually implemented using closures – data
structures containing all the information required to
evaluate the expression.
• Its opposite, eager evaluation, is the usual default in
a programming language in which arguments to a
function are always evaluated before the function is
applied.
Applications of Functional
Languages
• Lisp is used for artificial intelligence
applications
• Knowledge representation
• Machine learning
• Natural language processing
• Modeling of speech and vision
• Embedded Lisp interpreters add
programmability to some systems, such as
Emacs
• Scheme is used to teach introductory
programming at many universities
• FPLs are often used where rapid prototyping
is desired.
• Pure FPLs like Haskell are useful in contexts
requiring some degree of program verification
Summary: ILs vs
FPLs
Imperative Languages:
– Efficient execution
– Complex semantics
– Complex syntax
– Concurrency is programmer designed

Functional Languages:
– Simple semantics
– Simple syntax
– Inefficient execution
– Programs can automatically be made concurrent

You might also like