31
Functional Programming #FTW Adriano Bonat [email protected] @tanob

Functional Programming #FTW

Embed Size (px)

DESCRIPTION

An introduction to the basic concepts on functional programming, explaining why it is a hot topic for some years now, what it is and some suggestions of functional languages to be learned.

Citation preview

Page 1: Functional Programming #FTW

Functional Programming

#FTWAdriano Bonat

[email protected]@tanob

Page 2: Functional Programming #FTW

Why FP is a hot topic?

• Multicore CPUs

• VM based languages

• Great and easy to write DSLs

• Parsers

• Hardware design

• Expressiveness

Page 3: Functional Programming #FTW

What is it?

• Different programming paradigm

• OO

• Logic

• Procedural

• Functions are the main element in the language

Page 4: Functional Programming #FTW

Function applications“Functional programming is so called because a program consists entirely of functions. [...]

Typically the main function is defined in terms of other functions, which in turn are defined in terms of still more functions, until at the bottom level the functions are language primitives.”

John Hughes, 1989 - Why functional programming matters

Page 5: Functional Programming #FTW

OriginAlonzo Church developed Lambda

Calculus as part of his investigations on Math foundations

on 1936.

Page 6: Functional Programming #FTW

Lambda Calculus

• Variables

• Expressions (e1 e2)

• Lambda abstractions (λx. e)

Page 7: Functional Programming #FTW

Lambda Calculus (I)

• true = λxy. x

• false = λxy. y

• NOT a = (a)(false)(true)

• a AND b = (a)(b)(false)

• a OR b = (a)(true)(b)

• a XOR b = (a)((b)(false)(true))(b)

Page 8: Functional Programming #FTW

Basic concepts

• Pureness

• Pattern Matching

• Lazyness

• High Order Functions

• Currification (aka Partial Application)

Page 9: Functional Programming #FTW

Pureness

• No side-effects

• A function call can have no effect other than to compute its result

• Expressions can be evaluated at any time

• Programs are “referentially transparent”

Page 10: Functional Programming #FTW

Imperative stylefunction sum(elems: list of int) returns int {int total = 0;for elem in elems {total = total + elem;

}return total;

}

Page 11: Functional Programming #FTW

Imperative style

Computation method is variable assignment

function sum(elems: list of int) returns int {int total = 0;for elem in elems {total = total + elem;

}return total;

}

Page 12: Functional Programming #FTW

Functional stylePattern matching is cool!

Definition:sum [] = 0sum elem:rest = elem + sum rest

Application:sum [1,2,3,10]

Page 13: Functional Programming #FTW

Functional style

Computation method is function application

Definition:sum [] = 0sum elem:rest = elem + sum rest

Application:sum [1,2,3,10]

Page 14: Functional Programming #FTW

Lazyness

• aka “call by need”

• Expressions can be evaluated when necessary

• Allows the use of infinite lists

Page 15: Functional Programming #FTW

Lazyness (I)

Definition:even_numbers :: [Int]even_numbers = filter even [1..]

Application:take 5 even_numbers

Page 16: Functional Programming #FTW

Lazyness (II)

fibs :: [Int]fibs = 0 : 1 : zipWith (+) fibs (tail fibs)

From: http://en.wikipedia.org/wiki/Lazy_evaluation

Page 17: Functional Programming #FTW

High Order Functions

Functions which at least:

• Receive functions as parameters

• Return functions (aka curried functions)

Page 18: Functional Programming #FTW

High Order Functions (I)

map :: (a -> b) -> [a] -> [b]map f [] = []map f (x:xs) = f x : map f xs

Page 19: Functional Programming #FTW

Currification

sum :: Int -> Int -> Intsum x y = x + y

inc :: Int -> Intinc = sum 1

Page 20: Functional Programming #FTW

Where to go now?Suggestions:

• Haskell

• LISP

• Scheme

• Common LISP (CL)

• Clojure

• Erlang

• Scala

• F#

Page 21: Functional Programming #FTW

Haskell

• Pure n’ lazy

• Static typed

• Type classes

• Monads

• Lots of research & dev.

Page 22: Functional Programming #FTW

Haskell (I)

Freely available online:http://book.realworldhaskell.org/

Page 23: Functional Programming #FTW

LISP

• First functional language

• Lots of flavors

• Deal with parenthesis!

• Dynamic typed

• Homoiconic

Page 24: Functional Programming #FTW

Scheme

Freely available online:http://mitpress.mit.edu/sicp/

Page 25: Functional Programming #FTW

Clojure

• Runs on JVM

• Concurrency

Page 26: Functional Programming #FTW

Erlang

• Fault tolerant systems

• Concurrency

• Super easy!

• Dynamic typed

Page 27: Functional Programming #FTW

Scala

• Runs on JVM

• Several nice features

• Looks complex

• Static typed

Page 28: Functional Programming #FTW

F#

• Runs on .NET

• Result of MS’s investment on FP

• Simon Peyton Jones (Haskell contrib)

• Don Syme (F# creator)

• Erik Meijer (FP advocator, LINQ creator)

Page 29: Functional Programming #FTW

F# (I)

Pragmatic F# In Action

Josh Graham and Amanda Laucher

http://www.infoq.com/presentations/Pragmatic-F-Sharp-in-Action

Page 30: Functional Programming #FTW

Your Knowledge Portfolio

"Learn at least one new language every year. [...] Different languages solve the same problems in different ways. By learning several different approaches, you can help broaden your thinking and avoid getting stuck in a rut."

The Pragmatic Programmer

Page 31: Functional Programming #FTW

Functional Programming

#FTWAdriano Bonat

[email protected]@tanob