Introductory func prog


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introductory func prog

  1. 1. Introductory Functional Programming16.01.2013Valera Rozuvan
  2. 2. We all know what programming is #include <stdio.h> int main(void) {   printf("Hello  world!n");   return 0; }
  3. 3. Functional?A function is a rulewhich operates on aninput and produces anoutput.… programming ?
  4. 4. Wait... isnt 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. 5. Wrong!
  6. 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 isnt enough ...
  7. 7. procedural FORTRAN, C, Pascal, BASIC imperative object-oriented C++, Java, C#, VB.NET, Pythonprogramming logic Gödel, PROLOG declarative functional Lisp, Clojure, Erlang, Haskell
  8. 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. 9. Huh?
  10. 10. Example: factorialImperative: 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. 11. Functional programming languages are declarative
  12. 12. … in the beginning there was mathematics ...
  13. 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.
  14. 14. Wow... … hard.
  15. 15. Lambda calculus For Dummies
  16. 16. Applying FP to the *real* world?● I am a JavaScript person, so I will write JS! you have been warned
  17. 17. 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.
  18. 18. 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]); }
  19. 19. ● 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 );
  20. 20. ● 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]);
  21. 21. ● 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 );
  22. 22. Use functional programmingfor the greater good!
  23. 23. questions