Slide 1

Slide 1 text

An Introduction to Functional Programming Rebecca Skinner January 29, 2018 1

Slide 2

Slide 2 text

1 What is Functional Programming? 2

Slide 3

Slide 3 text

Introduction Functional Programming has been growing in popularity, especially over the last decade as more languages add first-class support for functional programming. In this presentation we’ll discuss 3

Slide 4

Slide 4 text

Introduction Functional Programming has been growing in popularity, especially over the last decade as more languages add first-class support for functional programming. In this presentation we’ll discuss • The history of functional programming • The difference between functional programming and functional languages • Introduce the Haskell programming language 3

Slide 5

Slide 5 text

Alonzo Church and Alan Turing Figure 1: Alan Turing Figure 2: Alonzo Church 4

Slide 6

Slide 6 text

Timeline of Functional Programming Functional programming has been gaining interest over the last decade, but it’s roots go back to the very beginnings of the field of Computer Science: • Alonzo Church discovered Lambda Calculus in the 1930’s • In 1958 John McCarthy created the first Lisp • In 1968 APL was created, it would go on to influence many functional languages • In 1973 ML was created • FP remains interesting mostly to academics for the next 40 years 5

Slide 7

Slide 7 text

Recent Interest in Functional Programming • 1998: The release of Half-Life means suddently nerds everywhere recognize the lambda symbol • 2005: Ruby starts to gain popularity, bringing closures, maps, and folds into popularity • 2007: Microsoft introduceds LINQ to .NET 3.5 • 2008: Java 8 introduces lambdas, and Java generics start to gain popularity • 2012: The Elm Programming Language is released • 2013: John Carmack mentions Haskell at QuakeCon • 2013: React is released, inciting interest in immutability in the frontend 6

Slide 8

Slide 8 text

History of Functional Programming Figure 3: Gordon Freeman: Crowbar and λ Calculus Enthusiast 7

Slide 9

Slide 9 text

Style, Not Languages Functional programming is an approach to software design, architecture, and philosophy. Functional programming isn’t about the language that you use. 8

Slide 10

Slide 10 text

What Makes Programming Functional? Fundamentally, functional programing is about modeling your program using pure mathematical functions- that is functions that always return the same output for a given input. There are a few other basics that are needed to do functional programming in any language: • First class functions • Pure functions • Immutability • Recursion • Strong separation between data structures and values 9

Slide 11

Slide 11 text

Related To Functional Programming Beyond just the core features needed to do FP, there are many features that commonly show up in tandem with functional code: • Pattern Matching • Controlled effects • Flexible type systems • Lazy Evaluation • Currying and Partial Application 10

Slide 12

Slide 12 text

What Makes a Functional Language A functional language is one that facilitates writing programs in a functional style. A pure functional language is one where functional programming is the dominant way of writing software in the language. • Haskell • Elm • Idris • Coq • Agda Pure Functional Languages • Java 8 • C++11 • Python • Ruby • Javascript Functional Languages • C • Go • Prolog • SQL • COBOL Non-Functional Languages 11

Slide 13

Slide 13 text

12

Slide 14

Slide 14 text

FP With Other Paradigms Functional style programming can work well when mixed with other programming paradigms like procedural, object oriented, or logic, or meta programming. Mixing FP with OOP is particularly popular, and numerous approaches have been developed to mix these two distinctive approaches to programming. 13

Slide 15

Slide 15 text

2 Why Functional Languages Now? 14

Slide 16

Slide 16 text

Modern Interest in an Old Approach After some diminishing interest in functional languages, especially lisp, as a means of developing AI in the 1980’s there was a drought of interest in FP outside of academia. Commercial and open source projects focused on single-dispatch class-based object oriented languages like C++ and Java, and on scripting languages like Python and Ruby. 15

Slide 17

Slide 17 text

A Surge of Interest In the late 2000’s and early 2010’s several factors contributed to a renewed interest in FP, including: • 12-factor apps modern cloud architecture • Map-Reduce, Hadoop, and the rise of Big Data • React.js • Scala, Clojure, Elm, and a resurgence in Erlang 16

Slide 18

Slide 18 text

17

Slide 19

Slide 19 text

FP + Cloud Cloud native applications share many of the same concerns as traditional functional programs. Whether it’s understanding how to scale and orchestrate processes like Erlang and Exliser, or threading state through a series of stateless data transformations, the techniques pioneered by functional programming can be of great benefit when building cloud applications. 18

Slide 20

Slide 20 text

3 Introducing Haskell 19

Slide 21

Slide 21 text

A Joke About Burritos 20

Slide 22

Slide 22 text

A Bit About Haskell Haskell is a pure functional programming language. It’s one of the more well known members of the ML family, and is perhaps the most widely used pure functional language in industry. 21

Slide 23

Slide 23 text

Some Facts About Haskell • Used at: Rackspace, Facebook, Microsoft, Target, Starbucks, and many others. • Over 10,000 packages available on Hackage • Runtime speed and memory footprint are on the order of efficient languages such as C, C++, and Java. • Typically compiled to native code or LLVM bytecode. • Easy interoperability with C and C++ 22

Slide 24

Slide 24 text

Some Other Facts About Haskell • Of the 10,000 packages on Hackage, some 8,000 of them are probably remnants of someone’s PhD Thesis • For every 5 haskell programs written, 6 monad tutorials are posted on someone’s blog • Readability is often considered somewhere between “obfuscated perl” and “c’thulu” • return in haskell is the most poorly chosen function name in the history of programming languages 23

Slide 25

Slide 25 text

Figure 4: Haskell’s Learning Curve, Visualized 24

Slide 26

Slide 26 text

3.1 Some Examples of Haskell 25

Slide 27

Slide 27 text

Hello World module Main where main = print " hello , world " 26

Slide 28

Slide 28 text

quicksort module Quicksort where quicksort [ ] = [ ] quicksort ( p : xs ) = ( quicksort lesser ) ++ [ p ] ++ ( quicksort greater ) where lesser = f i l t e r (< p ) xs greater = f i l t e r (>= p ) xs 27

Slide 29

Slide 29 text

module Echo where import System . IO import Control .Monad echo = do toEcho <− getLine unless ( null toEcho ) $ do putStrLn toEcho echo main = do putStrLn "Echo some words ! ( empty l i n e to q u i t ) " echo 28

Slide 30

Slide 30 text

Getting Started with Haskell Here are some handy pointers to get you started learning more abut haskell: • Haskell from First Principals • Haskell Wikibook • The Stack Tool • Haskell IDE (atom) • Haskell IDE (emacs) • What I Wish I Knew When Learning Haskell 29

Slide 31

Slide 31 text

4 Questions? 30