Introductory func prog

24
Introductory Functional Programming 16.01.2013 Valera Rozuvan

Transcript of Introductory func prog

Page 1: Introductory func prog

Introductory Functional Programming

16.01.2013

Valera Rozuvan

Page 2: Introductory func prog

We all know what programming is

#include <stdio.h>

int main(void) {

  printf("Hello world!\n");

  return 0;

}

Page 3: Introductory func prog

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

… programming ?

Page 4: Introductory func prog

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;

}

Page 5: Introductory func prog

Wrong!

Page 6: Introductory func prog

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 ...

Page 7: Introductory func prog

programming

imperative

declarative

proceduralFORTRAN, C, Pascal, BASIC

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

logicGödel, PROLOG

functionalLisp, Clojure, Erlang, Haskell

Page 8: Introductory func prog

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.

Page 9: Introductory func prog

Huh?

Page 10: Introductory func prog

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:

Page 11: Introductory func prog

Functional programming languages are declarative

Page 12: Introductory func prog

… in the beginning there was mathematics ...

Page 13: Introductory func prog

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.

Page 14: Introductory func prog
Page 15: Introductory func prog

Wow...

… hard.

Page 16: Introductory func prog

Lambda calculus For Dummies

Page 17: Introductory func prog

Applying FP to the *real* world?

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

youhavebeen

warned

Page 18: Introductory func prog

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.

Page 19: Introductory func prog

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]);}

Page 20: Introductory func prog

● 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);

Page 21: Introductory func prog

● 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]);

Page 22: Introductory func prog

● 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);

Page 23: Introductory func prog

Use functional programmingfor the greater good!

Page 24: Introductory func prog

questions