Functional Programming #FTW

Post on 12-May-2015

2.435 views 1 download

Tags:

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.

Transcript of Functional Programming #FTW

Functional Programming

#FTWAdriano Bonat

adrianob@gmail.com@tanob

Why FP is a hot topic?

• Multicore CPUs

• VM based languages

• Great and easy to write DSLs

• Parsers

• Hardware design

• Expressiveness

What is it?

• Different programming paradigm

• OO

• Logic

• Procedural

• Functions are the main element in the language

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

OriginAlonzo Church developed Lambda

Calculus as part of his investigations on Math foundations

on 1936.

Lambda Calculus

• Variables

• Expressions (e1 e2)

• Lambda abstractions (λx. e)

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)

Basic concepts

• Pureness

• Pattern Matching

• Lazyness

• High Order Functions

• Currification (aka Partial Application)

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”

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

}return total;

}

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;

}

Functional stylePattern matching is cool!

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

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

Functional style

Computation method is function application

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

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

Lazyness

• aka “call by need”

• Expressions can be evaluated when necessary

• Allows the use of infinite lists

Lazyness (I)

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

Application:take 5 even_numbers

Lazyness (II)

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

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

High Order Functions

Functions which at least:

• Receive functions as parameters

• Return functions (aka curried functions)

High Order Functions (I)

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

Currification

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

inc :: Int -> Intinc = sum 1

Where to go now?Suggestions:

• Haskell

• LISP

• Scheme

• Common LISP (CL)

• Clojure

• Erlang

• Scala

• F#

Haskell

• Pure n’ lazy

• Static typed

• Type classes

• Monads

• Lots of research & dev.

Haskell (I)

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

LISP

• First functional language

• Lots of flavors

• Deal with parenthesis!

• Dynamic typed

• Homoiconic

Scheme

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

Clojure

• Runs on JVM

• Concurrency

Erlang

• Fault tolerant systems

• Concurrency

• Super easy!

• Dynamic typed

Scala

• Runs on JVM

• Several nice features

• Looks complex

• Static typed

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)

F# (I)

Pragmatic F# In Action

Josh Graham and Amanda Laucher

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

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

Functional Programming

#FTWAdriano Bonat

adrianob@gmail.com@tanob