Sunteți pe pagina 1din 29

Functional Programming

Radu Răzvan Slăvescu

Technical University of Cluj-Napoca


Department of Computer Science
Outline

Functional Programming
Definition of Functional Programming
Where if FP used?
Brief history

Administrativia
Course overview
Bibliography
Evaluation

Basics of FP
Administrativia

Lectures
Assoc. prof. dr. eng. Radu Răzvan Slăvescu
Radu.Razvan.Slavescu@cs.utcluj.ro

Course Webpage
http://cs-gw.utcluj.ro/˜srazvan/fp/1819/fp1819.html
1. Lecture slides (in English) will be posted here every week
(or on Moodle)
2. Feedback appreciated
Definition
Functional Programming (FP) = a style of programming which
makes use of functions applied to arguments as basic blocks
for developing programs.

Remark
Functions have no side effects. In pure FP, there is no such a
thing as the ”state of the program”.

Remark
The arguments of a function could also be function calls, thus
building complex programs via function composition.
Definition

Program = Function
What is FP?

Definition (Functional language)


Functional language = a language that supports and
encourages this style of programming

Definition (Implementation)
Implementation = a software toolkit which offers its users the
possibility to write programs in a functional manner
Why FP?

Modern software challenges...


1. complexity increases
2. time of development should decrease
3. number of errors should decrease

...and solutions offered by Functional Programming


1. supports writing highly abstracted, clear and concise code
2. allows software reuse and rapid prototyping
3. permits reasoning about the program’s corectness
4. permits easy parallelization of the code
Where if FP used?

Functional Programming - some application domains


1. Erlang: WhatsApp (50 engineers, 900M users)
2. Haskell: Facebook (the Sigma anti-spam system)
3. Clojure: Cortex (a Deep Learning framework)
4. Artificial Intelligence tools and languages:
I research code: e.g. Grammatical Framework, a tool for
Natural Language Processing or Racer, a knowledge
representation system
I AIMA code
I influence in SOAR (a cognitive architecture for multi-agent
systems), in expert systems shells like CLIPS, JESS, LISA
5. tree-based Genetic Programming (e.g. Karoo GP):
programs as trees
Where if FP used?

Functional Programming Applications - further info


1. galois.com (critical systems)
2. commercial users of FP: http://cufp.org
3. https://www.lightbend.com/case-studies
4. projects developed in LISP: www.franz.com/success
A little bit of history

Lambda calculus: 1930s


I Lambda calculus - a theory by Alonzo Church about what
functions are able to compute
I could be seen as a minimal programming language
I virtualy every functional language is based on this theory
I usefull in many other fields, like expressing semantics of
sentences
A little bit of history

LISP (LISt Processing): 1950s


I functional language with ideas from lambda calculus
I allows variable assignment (unfortunately!)
I basic blocks: atoms, lists, forms
I nowadays dialects:
1. Common LISP
2. Scheme
3. Clojure
I implementations
1. CLISP
2. gcl (GNU Common LISP)
3. Allegro CL
A little bit of history

ML (Meta Language): 1970s


I featuring polymorphic types and type inference
I implementations:
1. polyML
2. Caml
3. Objective Caml
4. etc.
A little bit of history

Miranda family

Miranda means ”Admirable”


A little bit of history

Miranda family: Haskell (after Haskell Curry): 1987


I purely functional, with lazy evaluation
I standard: Haskell 2010 report (extends Haskell 98)
I implementations:
1. ghc (Glasgow Haskell Compiler)
2. hugs (Haskell User’s Gofer System)
3. etc.
Multiparadigm programming language

F#
I F# (Microsoft, for the .NET platform)
I Caml-like, but combines FP and OO approaches

C# and Python
I C#: immutability, function composition
I Python: lambdas, higher-order functions, lazy data-like
structures
Objectives

After completing this course you should:


I understand how programs can be seen as functions
I be able to write functions and functional code efficiently
I master concepts and techniques specific to FP:
I functions as parameters of some other functions;
anonymous functions; functions returned as results
I infinite data and lazy evaluation
I data regarded as functions
I be able to reason on functional programs in order to prove
their corectness or to properly transform them
I develop a basic understanding of Lambda expressions and
of the way they can be used to model a programming
language or the meaning of an expression
Lectures overview

Basics of programming in Haskell and ML


I identifiers and constants
I primitive data types
I expressions and operators
I functions and recursion
I evaluation (call-by-value, call-by-name, call-by-need)
I polymorphism, overloading and type inference
Lectures overview

Lists and trees


I list construction
I basic operations on lists
I list operators (generators, filters, list expressions)
I list-tree conversions
I trees and binary search trees
Lectures overview

FP specific elements
I higher-order functions (anonymous functions, partial
application)
I infinite data and lazy evaluation
I transformation and reasoning on programs
I elements of Lambda calculus
Lab overview

LISP lab: weeks 1 - 4


I LISP atoms, lists, forms
I primitive LISP functions
I defining LISP functions recursion
I LAMBDA-expressions and higher-order functions
I association lists and properties

ML: weeks 6 - 13
I practice writing Haskell and ML code
Bibliography

R. R. Slăvescu
Functional Programming - lecture notes.
Technical University of Cluj-Napoca, 2018.
I. A. Leţia, L. A. Negrescu, L. Negrescu
Programare funcţională, vol. I.
Ed. Albastră, Cluj-Napoca, Romania, 2006
I. A. Leţia
Programare funcţională.
UTPres, Cluj-Napoca, Romania, 1996
Bibliography - additional

G. Hutton
Programming in Haskell, 2nd edition.
Cambridge University Press, 2016
slides available from the course web page
Haskell wiki page
www.haskell.org
Bibliography - Lab

I. A. Leţia, E. Şt. Chifu, C. Cenan


Programare funcţională. Îndrumător de laborator.
Casa Cărţii de ştiinţă, Cluj-Napoca, Romania, 1999.
P. H. Winston, B. K. P. Horn
LISP, third edition.
Addison-Wesley, Reading, MA, 1990
A LISP tutorial
http://homepages.paradise.net.nz/milhous/
lisp.htm
Evaluation

How is your mark computed?


Weighted average:
I Written examination: 60%
I Lab test 1 (week 5, LISP): 20%
I Lab test 2 (week 14, polyML+Haskell): 20%

Condition for passing


In order to pass the exam: average ≥ 5 AND lab average ≥ 5
Written Exam

Exam
I 60 % of the final mark
I 2 problems/questions, one mark from 1 to 10 each
I If failed: repeated during the next session of exams
Two small Haskell examples

Erathostenes’ sieve

era(p:ns) = p:era [ n | n <- ns, n ‘mod‘ p > 0]

Quicksort

q [] = []
q (x:xs) = q [s | s <- xs, s<=x] ++
[x] ++
q [b | b <- xs, b>x]
Styles of interacting with computer

Conversational versus batch


1. conversational: enter functions from the keyboard and get
answers from the system
2. batch: functions stored in a file and loaded from there
Summary

I Functional Programming is a style of writting programs


which uses function calls as basic blocks of building
programs.
I Neither side effects, nor program state exist.
I Many functional languages and implementations are
available; among them, Haskell/ghc, ML/polyML and
LISP/gcl.
That’s all, folks!

Thanks for your attention...Questions?

S-ar putea să vă placă și