Functional Programming: Facts and Myths

Functional Vilnius meetup #1
Functional Vilnius meetup #1

Ignas Vyšniauskas (@yfyf)

2015-02-25

Hi

Lets talk about PLs.

Outline

Vague plan and goals of this talk:

  • Look at FP history and motivation
  • Describe FP features
  • Look at where we are today

Functional programming is a school of thought

Just like OOP.

OOP

Computational things are object-like things.

Made precise by a set of features, commonly:

  • Encapsulation
  • Inheritance
  • Subtyping
  • Late binding
  • Dynamic dispatch

Ruby / Java / C# / Python / Smalltalk are all “OOP”, but vary wildly.

FP

Programs are compositions of functions.

Concepts commonly associated with FP:

  • First-class functions
  • Declarative programming
  • Purity
  • Strong and static typing
  • Monads
  • Being slow
  • Being impractical

Programming paradigms
considered harmful

History

Some influences between FP PLs
Some influences between FP PLs

(Based on this graph.)

Escaping from Von Neumann

Escaping from Von Neumann

John Backus (1977)

  • FP language
  • Hints of declarative style
  • Concentrate on composing programs

λ-calculus (1930s)


(λx.M)N ⇒ M[x = N]

def f(x):
    [...]
    return M

>>> f(N)
M


(λx.x + 3)4 ⇒ (x + 3)[x = 4] = (4 + 3)

FP concepts

You are already a
functional programmer

First-class functions

def flip_args(f):
    def flipped(x, y):
        return f(y, x)

    return flipped
flip_args = lambda f: lambda x: lambda y: f(y, x)
flip :: (a -> b -> c) -> (b -> a -> c)
flip f = \x -> \y -> f y x

Purity

int getUserId(User u)
int getUserId(User u) {
    int id = u.id;
    launchNuclearWeapons();
    return id;
}

Purity

int getUserId(User u) {
    fetchIdFromDb(u);
}
f :: a -> b
getUserId :: User -> Int
getUserId :: User -> IO Int

Compositionality

First-class functions + Purity = Compositionality

compose :: (b -> c) -> (a -> b) -> a -> c
compose f g = \x -> f (g x)

Unix pipes are compositional

cat | grep | sort

Declarative programming

int i;
int arraySize = figureItOut(arr);
SomeData newarr[arraySize];
for (i = 0; i < arraySize; i++)
{
    newarr[i] = f(arr[i]);
}
maybeDestroyOldArr(arr);

vs.

newlst = map f lst

The Today

Some newish languages:

  • 2003: Scala
  • 2009: Go
  • 2012: Rust
  • 2014: (Apple) Swift

  • All explicit advertised as multi-paradigm.
  • All have extensive FP features

Each successive language incorporates, with a little cleaning up, all the features of its predecessors plus a few more. — John Backus, 1977

FP for everyone: a guide

  • “I hate static typing” => LISPs (Racket, Clojure), Erlang… Ruby?
  • “I need my objects” => F#, Scala, Swift
  • “I love Ruby” => Elixir
  • “I’m a lazy person” => Haskell
  • “I want to do front-end stuff” => Elm, Fay, GHCJS, Swift
  • “I want to do systems programming” => Ocaml, Rust
  • “I want to live in a JVM” => Clojure, Scala
  • “I love fancy types” => Ocaml, F#, Haskell, Idris, Agda
  • “I want to program rockets” => Coq

Conclusion

  • FP =/= Haskell
  • FP is not the “opposite of OOP”
  • Ideas from FP can make your code better
  • Your compiler will help you if you combine FP with static typing

Questions?

Addendum: laziness

first10 = take 10 $ getTwitterFollowers