16
Dr. Muhammed Al-Mulhe Dr. Muhammed Al-Mulhe m ICS535-101 ICS535-101 1 An Introduction to An Introduction to Functional Programming Functional Programming

Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

  • View
    219

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 11

An Introduction toAn Introduction toFunctional ProgrammingFunctional Programming

Page 2: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 22

TopicsTopics

IntroductionIntroduction

Mathematical FunctionsMathematical Functions

Fundamentals of Functional Programming Fundamentals of Functional Programming Languages Languages

Applications of Functional LanguagesApplications of Functional Languages

Comparison of Functional and Imperative Comparison of Functional and Imperative Languages Languages

The First F P Language: LISPThe First F P Language: LISP

COMMON LISPCOMMON LISP

Page 3: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 33

IntroductionIntroduction

The design of the imperative languages is based directly The design of the imperative languages is based directly on the on the von Neumann architecturevon Neumann architecture– Efficiency is the primary concern, rather than the Efficiency is the primary concern, rather than the

suitability of the language for software developmentsuitability of the language for software development

The design of the functional languages is based on The design of the functional languages is based on mathematical functionsmathematical functions– A solid theoretical basis that is also closer to the A solid theoretical basis that is also closer to the

user, but relatively unconcerned with the architecture user, but relatively unconcerned with the architecture of the machines on which programs will runof the machines on which programs will run

Page 4: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 44

Introduction (Contd.)Introduction (Contd.)What is the Functional Paradigm ?What is the Functional Paradigm ?

Program viewed as a collection of functions.Program viewed as a collection of functions.– Functional programming is a Functional programming is a stylestyle of programming of programming

in which the basic method of computation is the in which the basic method of computation is the application of functions to argumentsapplication of functions to arguments;;

– A functional language is one that A functional language is one that supportssupports and and encouragesencourages the functional style. the functional style.

Functional languages try to model Functional languages try to model mathematical functions as much as possible. mathematical functions as much as possible. There are no assignments; assignments are There are no assignments; assignments are bad and therefore banned! bad and therefore banned! Emphasize on simple and clean semantics. Emphasize on simple and clean semantics. Example languages: Scheme, Miranda, Example languages: Scheme, Miranda, Haskell, MLHaskell, ML

Page 5: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 55

Introduction (Contd.)Introduction (Contd.)The Functional Paradigm: Computational FoundationThe Functional Paradigm: Computational Foundation

FLs are based on a computation model called FLs are based on a computation model called lambda lambda calculuscalculus The lambda calculus is a simple mathematical theory The lambda calculus is a simple mathematical theory developed before the advent of digital computersdeveloped before the advent of digital computersThe lambda calculus was intended to be a foundation of The lambda calculus was intended to be a foundation of mathematics in the 1930smathematics in the 1930sThe lambda calculus has very few syntactic constructs The lambda calculus has very few syntactic constructs but it is but it is Turing completeTuring complete

– It can compute any function that can be computed by It can compute any function that can be computed by a computing devicea computing device

FLs are often considered as sugared versions of the FLs are often considered as sugared versions of the lambda calculuslambda calculus

Page 6: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 66

Introduction (Contd.)Introduction (Contd.)Functional Languages: Key FeaturesFunctional Languages: Key Features

High-level of abstractionHigh-level of abstraction– Functional languages are among the highest-level languages in Functional languages are among the highest-level languages in

present-day usepresent-day use

Clean and simple semanticsClean and simple semantics– Easier to reason about functional programs than imperative Easier to reason about functional programs than imperative

programsprograms

ExpressivityExpressivity– Support for rapid application developmentSupport for rapid application development

Support for concurrencySupport for concurrency

High orthogonalityHigh orthogonality– Few language constructs compared to imperative or OO Few language constructs compared to imperative or OO

languageslanguages

Lazy evaluation (for some of them)Lazy evaluation (for some of them)

Page 7: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 77

Mathematical FunctionsMathematical Functions

Def: A mathematical function is a Def: A mathematical function is a mappingmapping of of members of one set, called the members of one set, called the domain setdomain set, to , to another set, called the another set, called the range setrange set

A A lambda expressionlambda expression specifies the parameter(s) specifies the parameter(s) and the mapping of a function in the following and the mapping of a function in the following formform

(x) x * x * x(x) x * x * x

for the nameless function f(x) = x * x * x for the nameless function f(x) = x * x * x

Page 8: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 88

Mathematical Functions (Contd.)Mathematical Functions (Contd.)

Lambda expressions describe nameless Lambda expressions describe nameless functionsfunctions

Lambda expressions are applied to parameter(s) Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expressionby placing the parameter(s) after the expression

e.g. (e.g. ((x) x * x * x)(3)(x) x * x * x)(3)

which evaluates to 27which evaluates to 27

Page 9: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 99

Mathematical Functions (Contd.)Mathematical Functions (Contd.)Def:Def: A higher-order function, or A higher-order function, or functional formfunctional form, is one , is one that either takes functions as parameters or yields a that either takes functions as parameters or yields a function as its result, or bothfunction as its result, or bothMany kinds of functional forms, here are some:Many kinds of functional forms, here are some:

1. 1. Function CompositionFunction Composition– A functional form that takes two functions as A functional form that takes two functions as

parameters and yields a function whose value is the parameters and yields a function whose value is the first actual parameter function applied to the result of first actual parameter function applied to the result of the secondthe second

Form: h Form: h f ° g which means h (x) f ° g which means h (x) f ( g ( x)) f ( g ( x)) For f (x) For f (x) x * x * x and g (x) x * x * x and g (x) x + 3, x + 3, h h f ° g → f ° g → h(x) h(x) = f(g(x)) = g(x) * g(x) * g(x) = f(g(x)) = g(x) * g(x) * g(x)

=(x + 3)* (x + 3)* (x + 3)=(x + 3)* (x + 3)* (x + 3)

Page 10: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1010

Mathematical Functions Mathematical Functions (Contd.)(Contd.)

2. Construction2. Construction– A functional form that takes a list of functions as A functional form that takes a list of functions as

parameters and yields a list of the results of applying parameters and yields a list of the results of applying each of its parameter functions to a given parametereach of its parameter functions to a given parameter

Form: [f, g]Form: [f, g]

For f (x) For f (x) x * x * x and g (x) x * x * x and g (x) x + 3, x + 3,

[f, g] (4) yields (64, 7)[f, g] (4) yields (64, 7)

Page 11: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1111

Mathematical Functions (Contd.)Mathematical Functions (Contd.)

3. Apply-to-all3. Apply-to-all

– A functional form that takes a single function as a A functional form that takes a single function as a parameter and yields a list of values obtained by parameter and yields a list of values obtained by applying the given function to each element of a list of applying the given function to each element of a list of parametersparameters

Form: Form: For h (x) For h (x) x * x * x x * x * x

( h, (3, 2, 4)) yields (27, 8, 64)( h, (3, 2, 4)) yields (27, 8, 64)

Page 12: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1212

Why Functional Languages?Why Functional Languages?

The main reason we use functional programming is to The main reason we use functional programming is to reduce the time it takes to bring a product to the reduce the time it takes to bring a product to the market and to reduce the total effort required to design market and to reduce the total effort required to design and maintain our products. As and maintain our products. As functional programs functional programs are both shorter and more succinctare both shorter and more succinct than than corresponding programs in languages such as C, C+corresponding programs in languages such as C, C++, or Java, +, or Java, the number of errors made in the design is the number of errors made in the design is also greatly reducedalso greatly reduced

– Ericsson TelecomEricsson Telecom

Widely used for teaching introductory computer Widely used for teaching introductory computer science courses in European universities.science courses in European universities.

Page 13: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1313

Applications of Functional Applications of Functional LanguagesLanguages

APL is used for throw-away programsAPL is used for throw-away programs

LISP is used for artificial intelligenceLISP is used for artificial intelligence– Knowledge representationKnowledge representation– Machine learningMachine learning– Natural language processingNatural language processing– Modeling of speech and visionModeling of speech and vision

Scheme is used to teach introductory programming at Scheme is used to teach introductory programming at a significant number of universitiesa significant number of universities

Page 14: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1414

Comparing Fun. and Imp. Comparing Fun. and Imp. LanguagesLanguages

Imperative Languages:Imperative Languages:– Efficient executionEfficient execution– Complex semanticsComplex semantics– Complex syntaxComplex syntax– Concurrency is programmer designedConcurrency is programmer designed

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

Page 15: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1515

The First F P Language: LISPThe First F P Language: LISPData object typesData object types: originally only atoms and lists: originally only atoms and listsList formList form: parenthesized collections of sublists and/or : parenthesized collections of sublists and/or atoms atoms e.g., (A B (C D) E)e.g., (A B (C D) E)Originally, LISP was a typeless languageOriginally, LISP was a typeless languageLISP lists are stored internally as single-linked listsLISP lists are stored internally as single-linked listsLambda notation is used to specify functions and function Lambda notation is used to specify functions and function definitions. Function applications and data have the same definitions. Function applications and data have the same form.form.

e.g., If the list (A B C) is interpreted as data it ise.g., If the list (A B C) is interpreted as data it is a simple a simple list of threelist of three atoms, A, B, and Catoms, A, B, and C . If it is interpreted as a . If it is interpreted as a functionfunction application, it means that the function named A is application, it means that the function named A is applied to the two parameters, B and Capplied to the two parameters, B and CThe first LISP interpreter appeared only as a The first LISP interpreter appeared only as a demonstration of the universality of the computational demonstration of the universality of the computational capabilities of the notationcapabilities of the notation

Page 16: Dr. Muhammed Al-Mulhem ICS535-101 1 An Introduction to Functional Programming

Dr. Muhammed Al-MulhemDr. Muhammed Al-Mulhem ICS535-101ICS535-101 1616

COMMON LISPCOMMON LISPA combination of many of the features of the popular A combination of many of the features of the popular dialects of LISP around in the early 1980sdialects of LISP around in the early 1980s

A large and complex language--the opposite of SchemeA large and complex language--the opposite of Scheme

Includes:Includes:– records records – arrays arrays – complex numberscomplex numbers– character stringscharacter strings– powerful I/O capabilitiespowerful I/O capabilities– packages with access controlpackages with access control– imperative features like those of Schemeimperative features like those of Scheme– iterative control statementsiterative control statements