Docsity

functional programming, Essays (university) of Computer Science

Partial preview of the text

Download functional programming and more Essays (university) Computer Science in PDF only on Docsity!

The Joy Of

Functional

Programming

Steve Zhang

About Me

@CiaD team in Scotiabank

I am Passionate about:

TDD, XP, Clean Code

Software craftsmanship

Learning, deliberate

practice

The Joy of Functional

Programming

Pure

Elegant

Powerful

Anything that is

“effectively computable”

can be computed by a

Universal Turing

Machine.

  • Alan Turing, 1936

Alan Turing

Universal Turing Machine

λ calculus

Anything that is

“effectively computable”

can be computed by

via λ Calculus.

  • Church 1933,

Alonzo Church

λ calculus

Head

Body

(Expression)

Argument

λ calculus is equivalent to Turing

machines

Turing machine Lambda Calculus

Imperative Programming

 Assembly

 C / C++

 Java

 JavaScript

 OO …

Functional Programming

 Lisp

 Erlang

 Haskell

 Scala

 F# ….

What is Functional

Programming

A Programming paradigm based on

functions

Function is a first class citizen

can be a variable

passed as argument

return as a value

Roots in Lambda calculus

Functional Programming

Concepts

Pure functions

Referential Transparency

Immutability

Declarative Programming

Referential Transparency

Same input always get the same result

Every subexpression can be substituted

by any other that’s equal to it in value

Easier to reason

RT functions will NOT

print on screen

output logs

Immutability

Data can’t be changed after it’s been

created

No assignment

No shared state

Quicksort In C

void quicksort (int *A, int len ) {

if ( len < 2) return ;

int pivot = A [ len / 2] ;

}

int i, j;

for ( i = 0 , j = len - 1 ; ; i++, j-- ) {

while ( A [ i ] < pivot ) i++;

while ( A [ j ] > pivot ) j--;

if ( i >= j ) break ;

int temp = A [ i ] ;

A [ i ] = A [ j ] ;

A [ j ] = temp;

quicksort ( A, i ) ;

quicksort ( A + i, len - i ) ;

}

Quicksort in Haskell

quicksort :: Ord a => [a] ->

[a] quicksort [] = []

quicksort (p:xs) = (quicksort lesser) ++ [p] ++ (quicksort greater)

where

lesser = filter (< p) xs

greater = filter (>= p) xs