# Why Functional Programming?

@volpegabriel87

@gvolpe

0

@volpegabriel87

• Local perspective
• Progress oriented
• Detail
• Vast memory
• Pretty reliable
• Machine language

### Machine / Human impedance mismatch

@gvolpe

"A crash course in Category Theory" by Bartosz Milewski (2017)

• Global perspective
• Goal oriented
• Idea
• Limited memory
• Error prone
• Mathematics

# λ-calculus

@volpegabriel87

@gvolpe

"A set of postulates for the foundation of logic" by Alonzo Church (1932)

A universal model of computation that can be used to simulate any Turing Machine

A formal system for expressing a computation based on function abstraction and application

# LISP

@volpegabriel87

@gvolpe

"LISP specification" by John McCarthy (1958)

Originally created as a practical mathematical notation for computer programs, influenced by the Lambda Calculus system

He also invented garbage collection (implemented in LISP)

# Erlang

@volpegabriel87

@gvolpe

Developed by Ericsson ( Joe Armstrong, Robert Virding, and Mike Williams) in 1986

Distributed, fault-tolerant and highly available

Open Telecom Platform (OTP)

@volpegabriel87

@gvolpe

First versions didn't support I/O

"Escape from the ivory tower: the Haskell journey" by Simon P. Jones

# Functional Programming

@volpegabriel87

@gvolpe

"Why functional programming matters" by John Hughes (1990)

- Higher-order functions

- Lazy evaluation

# Functional Programming

@volpegabriel87

@gvolpe

Sequential computations

# Functional Programming

@volpegabriel87

@gvolpe

``````foo :: Maybe Int
foo = Just 1

bar :: Maybe Int
bar = Just 2

sum :: Maybe Int
sum = foo >>= \x -> bar >>= \y -> return (x + y)

sum' :: Maybe Int
sum' = do
x <- foo
y <- bar
return (x + y)``````

# Functional Programming

@volpegabriel87

@gvolpe

"Imperative functional programming" by Simon P. Jones and Philip Wadler (1992)

- Connecting I/O operations

Ability to mix imperative and purely functional programming, without ruining either

# Functional Programming

@volpegabriel87

@gvolpe

``````toUpper :: Char -> Char
getChar :: IO Char
putChar :: Char -> IO ()``````
``````(>>=) :: IO a -> (a -> IO b) -> IO b
return :: a -> IO a``````
``getChar >>= (\a -> putChar a)``

@volpegabriel87

@gvolpe

# Console program

``````main :: IO ()
main = do
putStrLn ("Welcome " <> n)``````
``````val program: IO[Unit] =
for {
_ <- IO(println(s"Welcome \$n"))
} yield ()
``````
``````main :: Effect Unit
main = log "Hello, World!"``````

# Functional Programming

@volpegabriel87

@gvolpe

• PureScript
• Scala
• ELM
• OCaml
• Nix

DevOps industry is choosing Nix for its reproducible builds and immutability

# Functional Programming

@volpegabriel87

@gvolpe

### Nixpkgs

0

``````\$ curl https://nixos.org/nix/install | sh
\$ nix-env -i hello
\$ which hello
/home/user/.nix-profile/bin/hello
\$ hello
Hello, world!
\$ nix-env -e hello
\$ nix-shell -p hello``````

# Functional Programming

@volpegabriel87

@gvolpe

FP languages can be reasoned about mathematically

# Functional Programming

@volpegabriel87

@gvolpe

• Microsoft (serialization system in Haskell)
• New York Times (image processing in Haskell)

@volpegabriel87

@gvolpe

@volpegabriel87

@gvolpe

@volpegabriel87

@gvolpe

By Gabriel Volpe

# why-fp

Why Functional Programming?

• 2,580