a brief introduction to functional programming

16
A brief introduction to functional programming Sebastian Wild Stylight GmbH October 31, 2012

Upload: sebastian-wild

Post on 13-Jul-2015

142 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: A brief introduction to functional programming

A brief introduction to functional programming

Sebastian Wild

Stylight GmbH

October 31, 2012

Page 2: A brief introduction to functional programming

Functional programming languages

Some programming conceptsVariables and FunctionsCurryingFunctions as parametersAccumulators

Advanced examples

Summary

Page 3: A brief introduction to functional programming

And that language is fast. I mean, really astoundinglyfast, which is why it is so evil. It’s like that psychoticgirlfriend that was amazing in bed. You keep trying tobreak up with her, but she seduces you right back intothat abusive relationship.

from brool.comArticle: The Genius of Python, The Agony of OCaml

Page 4: A brief introduction to functional programming

Functional programming languages

I PureHaskell, Charity, Clean, Curry, Hope, Miranda

I ImpureErlang, F#, Lisp, ML (Standard ML, OCaml), Scala,Spreadsheets

Page 5: A brief introduction to functional programming

First program

# p r i n t s t r i n g ” H e l l o , World ! ” ; ;H e l l o , World : u n i t = ( )

Page 6: A brief introduction to functional programming

Variables and Functions

# l e t a = 5 ; ;v a l a : i n t = 5

# l e t a = 5 + 3 ; ;v a l a : i n t = 8

(∗ ! ! A t t e n t i o n ! ! St rong t y p e system ∗)# l e t a = 5 + 3 . 0 ; ;

Page 7: A brief introduction to functional programming

Variables and Functions

# l e t a = ( 5 , ( ) , ” H e l l o ” ) ; ;v a l a : i n t ∗ u n i t ∗ s t r i n g = ( 5 , ( ) , ” H e l l o ”)

# l e t f x = x ∗ x ; ;v a l f : i n t −> i n t = <fun>

Page 8: A brief introduction to functional programming

Currying

# l e t f ( x , y ) = x ∗ y ; ;v a l f : i n t ∗ i n t −> i n t = <fun>

Page 9: A brief introduction to functional programming

Currying

# l e t f x y = x ∗ y ; ;v a l f : i n t −> i n t −> i n t = <fun>

(∗ Types a r e r i g h t b r a c k e t e d ∗)i n t −> ( i n t −> i n t )

Page 10: A brief introduction to functional programming

Currying

# l e t f x y = x ∗ y ; ;

# l e t a = f 5 ; ;v a l a : i n t −> i n t = <fun>

# l e t b = a 6 ; ;v a l b : i n t = 30

(∗ F u n c t i o n c a l l s a r e l e f t b r a c k e t e d ∗)# v a l b = ( f 5) 6 ; ;v a l b : i n t = 30

Page 11: A brief introduction to functional programming

Functions as parameters

# l e t a = [ 1 ; 2 ; 3 ; 4 ] ; ;v a l a : i n t l i s t = [ 1 ; 2 ; 3 ; 4 ]

# l e t r e c map f s =match s w i t h

[ ] −> [ ]| x : : x s −> ( f x ) : : ( map f xs ) ; ;

v a l map : ( ’ a −> ’ b ) −> ’ a l i s t −> ’ b l i s t = <fun>

# l e t b = map ( fun v −> 2 ∗ v ) a ; ;v a l b : i n t l i s t = [ 2 ; 4 ; 6 ; 8 ]

Page 12: A brief introduction to functional programming

Functions as parameters

L i s t . f o l d l e f t : ( ’ a −> ’ b −> ’ a ) −> ’ a −> ’ b l i s t −> ’ a

L i s t . f o l d l e f t f a [ b1 ; . . . ; bn ]f ( . . . ( f ( f a b1 ) b2 ) . . . )

# L i s t . f o l d l e f t (+) 0 [ 1 ; 2 ; 3 ; 4 ; 5 ; 6 ] ; ;− : i n t = 21

Page 13: A brief introduction to functional programming

Accumulators

Faculty of n:

# l e t r e c f a c n =i f n <= 0 then 1e l s e n ∗ f a c ( n − 1 ) ; ;

v a l f a c : i n t −> i n t = <fun>

Page 14: A brief introduction to functional programming

Accumulator

Tail recursive:

# l e t f a c n =l e t r e c fac ’ acc n =

i f n <= 0 then acce l s e fac ’ ( n∗ acc ) ( n−1) i n

fac ’ 1 n ; ;v a l f a c : i n t −> i n t = <fun>

Page 15: A brief introduction to functional programming

Advanced examples

http://www.ffconsultancy.com/ocaml/bunny/index.htmlhttp://www.ffconsultancy.com/ocaml/ray tracer/index.html

Page 16: A brief introduction to functional programming

Summary

I No side effects

I Strong type system

I Mathematical functions can be expressed very easily

I Steep learning curve

https://github.com/wildsebastian/AlgorithmsDataStructures