functional programming, Essays (university) of Computer Science

Haskell functional programming

Typology: Essays (university)

2017/2018

Uploaded on 05/03/2018

zahraa-tishreen
zahraa-tishreen šŸ‡ŖšŸ‡¬

1 document

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
The Joy Of
Functional
Programming
Steve Zhang
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

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