24
Introductory Functional Programming 16.01.2013 Valera Rozuvan

Introduction Functional Programming - Tech Hangout #11 - 2013.01.16

Embed Size (px)

Citation preview

Introductory Functional Programming

16.01.2013Valera Rozuvan

We all know what programming is

#include <stdio.h>

int main(void) {

printf("Hello world!\n");

return 0;

}

Functional?A function is a rule which operates on an input and produces an output.

… programming ?

Wait... isn't this

already functional programming? It is after all a program that defines a function main(), which is then executed ...

#include <stdio.h>

int main(void) {

printf("Hello world!\n");

return 0;

}

Wrong!

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data.

… just having functions isn't enough ...

programming

imperative

declarative

proceduralFORTRAN, C, Pascal, BASIC

object-orientedC++, Java, C#, VB.NET, Python

logicGödel, PROLOG

functionalLisp, Clojure, Erlang, Haskell

Imperative vs. Declarative

● Imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. Imperative programs define sequences of commands for the computer to perform.

● Declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow.

Huh?

Example: factorial

def factorial(x)

res = 1

while (x > 1)

res = res*x

x = x-1

end

return res

end

x = 10

print(factorial(x))

def factorial(x)

if x < 2

return 1

else

return x*factorial(x-1)

end

end

print(factorial(10))

Imperative: Declarative:

Functional programming languages are declarative

… in the beginning there was mathematics ...

Lambda calculus

● The λ-calculus calculus was introduced by mathematician Alonzo Church in the 1930s.

● The λ-calculus treats functions "anonymously", without giving them explicit names.

● In λ-calculus, functions are taken to be 'first class values', so functions may be used as the inputs and returned as outputs from other functions.

● λ-calculus – a formal system for function definition, application, and recursion.

Wow...

… hard.

Lambda calculus For Dummies

Applying FP to the *real* world?

● I am a JavaScript person, so I will write JS!

youhavebeen

warned

Can you do FP in JS?

● If you define functional language as the language that supports first class functions and lambdas, then yes, JavaScript *is* a functional language.

● JavaScript supports passing around functions as variables.

● JavaScript supports anonymous functions.

Short answer – yes! If you start to argue – no.

A short demo

● If you have any good taste at all, one ugly detail must be starting to bother you - the endlessly repeated for loop going over an array.

function printArray(array) { for (var i = 0; i < array.length; i++) print(array[i]);}

● But what if we want to do something other than print? Because 'doing something' is really a function, and functions are also values, we can pass our action as a function value.

function forEach(array, action) { for (var i = 0; i < array.length; i++) action(array[i]);}

forEach( ["Wampeter", "Foma", "Granfalloon"], print);

● And by making use of an anonymous function, something just like a for loop can be written with less useless details.

function sum(numbers) { var total = 0;

forEach(numbers, function (number) { total += number; });

return total;}show(sum[1, 10, 100]);

● On the whole, using more abstract (or 'higher level') constructs results in more information and less noise. Compare the following:

var paragraphs = archive[today].split("\n");for (var i = 0; i < paragraphs.length; i++) processParagraph(paragraphs[i]);

versus

forEach( archive[today].split("\n"), ProcessParagraph);

Use functional programmingfor the greater good!

questions