Functional Programming
Functional Programming
Functional Programming
Functional Programming
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