# Introduction Functional Programming - Tech Hangout #11 - 2013.01.16

Innovecs
16. Apr 2013
1 von 24

### Introduction Functional Programming - Tech Hangout #11 - 2013.01.16

• 1. Introductory Functional Programming 16.01.2013 Valera Rozuvan
• 2. We all know what programming is #include <stdio.h> int main(void) {   printf("Hello  world!n");   return 0; }
• 3. Functional? A function is a rule which operates on an input and produces an output. … programming ?
• 4. Wait... isn't this #include <stdio.h> int main(void) {   printf("Hello world!n");   return 0; } already functional programming? It is after all a program that defines a function main(), which is then executed ...
• 5. Wrong!
• 6. 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 ...
• 7. procedural FORTRAN, C, Pascal, BASIC imperative object-oriented C++, Java, C#, VB.NET, Python programming logic Gödel, PROLOG declarative functional Lisp, Clojure, Erlang, Haskell
• 8. 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.
• 9. Huh?
• 10. Example: factorial Imperative: Declarative: def factorial(x) def factorial(x)   res = 1   if x < 2   while (x > 1)     return 1     res = res*x   else     x = x­1     return x*factorial(x­1)   end   end   return res end end print(factorial(10)) x = 10 print(factorial(x))
• 11. Functional programming languages are declarative
• 12. … in the beginning there was mathematics ...
• 13. 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.
• 15. Wow... … hard.
• 16. Lambda calculus For Dummies
• 17. Applying FP to the *real* world? ● I am a JavaScript person, so I will write JS! you have been warned
• 18. 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.
• 19. 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]); }
• 20. 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 );
• 21. 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]);
• 22. 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 );
• 23. Use functional programming for the greater good!
• 24. questions