SlideShare uma empresa Scribd logo
1 de 24
Introductory Functional
           Programming

16.01.2013
Valera 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
     #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 ...
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 ...
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
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
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))
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!




                                                   you
                                                  have
                                                  been
                                                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 programming
for the greater good!
questions

Mais conteúdo relacionado

Mais procurados

Functional programing in Javascript (lite intro)
Functional programing in Javascript (lite intro)Functional programing in Javascript (lite intro)
Functional programing in Javascript (lite intro)Nikos Kalogridis
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript FundamentalsSrdjan Strbanovic
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programmingKonrad Szydlo
 
Interpreter Design Pattern in Javascript
Interpreter Design Pattern in JavascriptInterpreter Design Pattern in Javascript
Interpreter Design Pattern in JavascriptDmytro Verbovyi
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonAnoop Thomas Mathew
 
Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_eShikshak
 
An Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptAn Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptDoug Sparling
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpprajshreemuthiah
 
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD? WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD? reactima
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scalaStratio
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming Kamal Acharya
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageAzilen Technologies Pvt. Ltd.
 

Mais procurados (19)

Functional programing in Javascript (lite intro)
Functional programing in Javascript (lite intro)Functional programing in Javascript (lite intro)
Functional programing in Javascript (lite intro)
 
Functional JavaScript Fundamentals
Functional JavaScript FundamentalsFunctional JavaScript Fundamentals
Functional JavaScript Fundamentals
 
Scala qq
Scala qqScala qq
Scala qq
 
5.functions
5.functions5.functions
5.functions
 
Functional go
Functional goFunctional go
Functional go
 
ALGOL ailesi programlama dilleri
ALGOL ailesi programlama dilleriALGOL ailesi programlama dilleri
ALGOL ailesi programlama dilleri
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
 
Interpreter Design Pattern in Javascript
Interpreter Design Pattern in JavascriptInterpreter Design Pattern in Javascript
Interpreter Design Pattern in Javascript
 
Scala functions
Scala functionsScala functions
Scala functions
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 
Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_Lecture 3 getting_started_with__c_
Lecture 3 getting_started_with__c_
 
Type Checking JavaScript
Type Checking JavaScriptType Checking JavaScript
Type Checking JavaScript
 
An Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptAn Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with Javascript
 
Pointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cppPointers,virtual functions and polymorphism cpp
Pointers,virtual functions and polymorphism cpp
 
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD? WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
 
Storage Class in C Progrmming
Storage Class in C Progrmming Storage Class in C Progrmming
Storage Class in C Progrmming
 
Pointers & functions
Pointers &  functionsPointers &  functions
Pointers & functions
 
Developer’s viewpoint on swift programming language
Developer’s viewpoint on swift programming languageDeveloper’s viewpoint on swift programming language
Developer’s viewpoint on swift programming language
 

Destaque

2010冬季亞成鳥太加縱走new
2010冬季亞成鳥太加縱走new2010冬季亞成鳥太加縱走new
2010冬季亞成鳥太加縱走newfishliu
 
內本鹿小學的緣起
內本鹿小學的緣起內本鹿小學的緣起
內本鹿小學的緣起fishliu
 
亞成鳥分享@陸府
亞成鳥分享@陸府亞成鳥分享@陸府
亞成鳥分享@陸府fishliu
 
Maxxconnect Presentation
Maxxconnect PresentationMaxxconnect Presentation
Maxxconnect PresentationShankarNabar
 
Hugo Zhao's Portfolio 2014 - short version
Hugo Zhao's Portfolio 2014 - short versionHugo Zhao's Portfolio 2014 - short version
Hugo Zhao's Portfolio 2014 - short versionrsunrain
 
南湖北稜77事件報告
南湖北稜77事件報告南湖北稜77事件報告
南湖北稜77事件報告fishliu
 
第七梯亞成鳥 太加縱走
第七梯亞成鳥 太加縱走第七梯亞成鳥 太加縱走
第七梯亞成鳥 太加縱走fishliu
 
2010冬季亞成鳥-太加縱走
2010冬季亞成鳥-太加縱走2010冬季亞成鳥-太加縱走
2010冬季亞成鳥-太加縱走fishliu
 
Asang內本鹿-布農山林生活的追尋與再現(村落文化發展提案)
Asang內本鹿-布農山林生活的追尋與再現(村落文化發展提案)Asang內本鹿-布農山林生活的追尋與再現(村落文化發展提案)
Asang內本鹿-布農山林生活的追尋與再現(村落文化發展提案)fishliu
 
[季刊分享]重返內本鹿 布農族尋根之旅
[季刊分享]重返內本鹿 布農族尋根之旅[季刊分享]重返內本鹿 布農族尋根之旅
[季刊分享]重返內本鹿 布農族尋根之旅fishliu
 
Thucydides - /by Igor Rozdobudko
Thucydides - /by Igor RozdobudkoThucydides - /by Igor Rozdobudko
Thucydides - /by Igor RozdobudkoOleksandr Khomenko
 
Meeting 1 Training And Techniques In 16th Century Venice
Meeting 1 Training And Techniques In 16th Century VeniceMeeting 1 Training And Techniques In 16th Century Venice
Meeting 1 Training And Techniques In 16th Century VeniceAnthony's Fine Art
 
Shankar Nabar Consulting Show
Shankar Nabar Consulting ShowShankar Nabar Consulting Show
Shankar Nabar Consulting ShowShankarNabar
 
social media promotion- Mister Moon Media (A Shankar Nabar Company)
social media promotion- Mister Moon Media (A Shankar Nabar Company)social media promotion- Mister Moon Media (A Shankar Nabar Company)
social media promotion- Mister Moon Media (A Shankar Nabar Company)ShankarNabar
 
The earth and its neighbours
The earth and its neighboursThe earth and its neighbours
The earth and its neighboursBipin Tripathi
 
IxD & UX Design - Personifying Digital Interactions
IxD & UX Design - Personifying Digital InteractionsIxD & UX Design - Personifying Digital Interactions
IxD & UX Design - Personifying Digital InteractionsJayan Narayanan
 
Tiger-project for High School Students
Tiger-project for High School StudentsTiger-project for High School Students
Tiger-project for High School StudentsBipin Tripathi
 

Destaque (18)

2010冬季亞成鳥太加縱走new
2010冬季亞成鳥太加縱走new2010冬季亞成鳥太加縱走new
2010冬季亞成鳥太加縱走new
 
內本鹿小學的緣起
內本鹿小學的緣起內本鹿小學的緣起
內本鹿小學的緣起
 
亞成鳥分享@陸府
亞成鳥分享@陸府亞成鳥分享@陸府
亞成鳥分享@陸府
 
Maxxconnect Presentation
Maxxconnect PresentationMaxxconnect Presentation
Maxxconnect Presentation
 
Hugo Zhao's Portfolio 2014 - short version
Hugo Zhao's Portfolio 2014 - short versionHugo Zhao's Portfolio 2014 - short version
Hugo Zhao's Portfolio 2014 - short version
 
南湖北稜77事件報告
南湖北稜77事件報告南湖北稜77事件報告
南湖北稜77事件報告
 
第七梯亞成鳥 太加縱走
第七梯亞成鳥 太加縱走第七梯亞成鳥 太加縱走
第七梯亞成鳥 太加縱走
 
2010冬季亞成鳥-太加縱走
2010冬季亞成鳥-太加縱走2010冬季亞成鳥-太加縱走
2010冬季亞成鳥-太加縱走
 
Asang內本鹿-布農山林生活的追尋與再現(村落文化發展提案)
Asang內本鹿-布農山林生活的追尋與再現(村落文化發展提案)Asang內本鹿-布農山林生活的追尋與再現(村落文化發展提案)
Asang內本鹿-布農山林生活的追尋與再現(村落文化發展提案)
 
[季刊分享]重返內本鹿 布農族尋根之旅
[季刊分享]重返內本鹿 布農族尋根之旅[季刊分享]重返內本鹿 布農族尋根之旅
[季刊分享]重返內本鹿 布農族尋根之旅
 
Thucydides - /by Igor Rozdobudko
Thucydides - /by Igor RozdobudkoThucydides - /by Igor Rozdobudko
Thucydides - /by Igor Rozdobudko
 
Meeting 1 Training And Techniques In 16th Century Venice
Meeting 1 Training And Techniques In 16th Century VeniceMeeting 1 Training And Techniques In 16th Century Venice
Meeting 1 Training And Techniques In 16th Century Venice
 
Shankar Nabar Consulting Show
Shankar Nabar Consulting ShowShankar Nabar Consulting Show
Shankar Nabar Consulting Show
 
social media promotion- Mister Moon Media (A Shankar Nabar Company)
social media promotion- Mister Moon Media (A Shankar Nabar Company)social media promotion- Mister Moon Media (A Shankar Nabar Company)
social media promotion- Mister Moon Media (A Shankar Nabar Company)
 
Be HTML5-ready today
Be HTML5-ready todayBe HTML5-ready today
Be HTML5-ready today
 
The earth and its neighbours
The earth and its neighboursThe earth and its neighbours
The earth and its neighbours
 
IxD & UX Design - Personifying Digital Interactions
IxD & UX Design - Personifying Digital InteractionsIxD & UX Design - Personifying Digital Interactions
IxD & UX Design - Personifying Digital Interactions
 
Tiger-project for High School Students
Tiger-project for High School StudentsTiger-project for High School Students
Tiger-project for High School Students
 

Semelhante a Introductory func prog

Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional ProgrammingGeison Goes
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingLex Sheehan
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mindSander Mak (@Sander_Mak)
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingRichardWarburton
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Bryan O'Sullivan
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
DataWeave 2.0 Language Fundamentals
DataWeave 2.0 Language FundamentalsDataWeave 2.0 Language Fundamentals
DataWeave 2.0 Language FundamentalsJoshua Erney
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture TwoAngelo Corsaro
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesDominic Graefen
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming ParadigmsDirecti Group
 
Functional programming
Functional programmingFunctional programming
Functional programmingijcd
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScriptWill Livengood
 
About Functional Programming
About Functional ProgrammingAbout Functional Programming
About Functional ProgrammingAapo Kyrölä
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojureJuan-Manuel Gimeno
 
Functional reactive programming
Functional reactive programmingFunctional reactive programming
Functional reactive programmingHung Hoang
 
pure-functional-programming.pdf
pure-functional-programming.pdfpure-functional-programming.pdf
pure-functional-programming.pdfPuneetChaturvedi23
 

Semelhante a Introductory func prog (20)

Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional ProgrammingGo Beyond Higher Order Functions: A Journey into Functional Programming
Go Beyond Higher Order Functions: A Journey into Functional Programming
 
Scala: functional programming for the imperative mind
Scala: functional programming for the imperative mindScala: functional programming for the imperative mind
Scala: functional programming for the imperative mind
 
Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Twins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional ProgrammingTwins: Object Oriented Programming and Functional Programming
Twins: Object Oriented Programming and Functional Programming
 
Real World Haskell: Lecture 1
Real World Haskell: Lecture 1Real World Haskell: Lecture 1
Real World Haskell: Lecture 1
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
DataWeave 2.0 Language Fundamentals
DataWeave 2.0 Language FundamentalsDataWeave 2.0 Language Fundamentals
DataWeave 2.0 Language Fundamentals
 
PARADIGM IT.pptx
PARADIGM IT.pptxPARADIGM IT.pptx
PARADIGM IT.pptx
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
LISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love ParanthesesLISP: How I Learned To Stop Worrying And Love Parantheses
LISP: How I Learned To Stop Worrying And Love Parantheses
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional Programming in JavaScript
Functional Programming in JavaScriptFunctional Programming in JavaScript
Functional Programming in JavaScript
 
About Functional Programming
About Functional ProgrammingAbout Functional Programming
About Functional Programming
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
 
Dart workshop
Dart workshopDart workshop
Dart workshop
 
Functional reactive programming
Functional reactive programmingFunctional reactive programming
Functional reactive programming
 
pure-functional-programming.pdf
pure-functional-programming.pdfpure-functional-programming.pdf
pure-functional-programming.pdf
 

Introductory func prog

  • 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 ...
  • 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.
  • 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))
  • 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.
  • 14.
  • 15. Wow... … hard.
  • 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!