SlideShare a Scribd company logo
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.
Introduction Functional Programming - Tech Hangout #11 - 2013.01.16
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

More Related Content

PPTX
Dev Concepts: Functional Programming
PDF
Some basic FP concepts
PPTX
Functional programming
PDF
Python functional programming
PPTX
Learn To Code: Introduction to c
PPTX
Scala overview
PDF
Functional Python Webinar from October 22nd, 2014
PPTX
Es6 features part 1
Dev Concepts: Functional Programming
Some basic FP concepts
Functional programming
Python functional programming
Learn To Code: Introduction to c
Scala overview
Functional Python Webinar from October 22nd, 2014
Es6 features part 1

What's hot (19)

PDF
Functional programing in Javascript (lite intro)
PDF
Functional JavaScript Fundamentals
PDF
Scala qq
PPTX
5.functions
PDF
Functional go
PPT
ALGOL ailesi programlama dilleri
PDF
Introduction to functional programming
PPTX
Interpreter Design Pattern in Javascript
PPT
Scala functions
PDF
Thinking in Functions: Functional Programming in Python
PDF
Lecture 3 getting_started_with__c_
PDF
Type Checking JavaScript
PPTX
An Introduction to Functional Programming with Javascript
PPTX
Pointers,virtual functions and polymorphism cpp
PPTX
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
PDF
Functional programming in scala
PPTX
Storage Class in C Progrmming
PDF
Pointers & functions
PPTX
Developer’s viewpoint on swift programming language
Functional programing in Javascript (lite intro)
Functional JavaScript Fundamentals
Scala qq
5.functions
Functional go
ALGOL ailesi programlama dilleri
Introduction to functional programming
Interpreter Design Pattern in Javascript
Scala functions
Thinking in Functions: Functional Programming in Python
Lecture 3 getting_started_with__c_
Type Checking JavaScript
An Introduction to Functional Programming with Javascript
Pointers,virtual functions and polymorphism cpp
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
Functional programming in scala
Storage Class in C Progrmming
Pointers & functions
Developer’s viewpoint on swift programming language
Ad

Similar to Introduction Functional Programming - Tech Hangout #11 - 2013.01.16 (20)

PDF
Functional Programming #FTW
PPTX
Introduction to Functional Programming
PDF
Introduction to functional programming (In Arabic)
PDF
Functional programming
PPTX
Intro f# functional_programming
PPTX
Functional reactive programming
PPTX
Functional Programming.pptx
PPTX
Functional Programming Fundamentals
PPTX
Unraveling the mystery of monads
PPTX
Functional Programming Concepts for Imperative Programmers
PDF
JavaScript for ABAP Programmers - 7/7 Functional Programming
PPTX
Plc part 3
KEY
Scala: functional programming for the imperative mind
PPTX
Functional Programming in Swift
PPTX
Functional Programming Fundamentals
PPTX
The joy of functional programming
KEY
Functional programming in clojure
PDF
Functional Programming for Busy Object Oriented Programmers
PPS
Presentation of GetTogether on Functional Programming
PDF
Functional programming in Python 1st Edition David Mertz
Functional Programming #FTW
Introduction to Functional Programming
Introduction to functional programming (In Arabic)
Functional programming
Intro f# functional_programming
Functional reactive programming
Functional Programming.pptx
Functional Programming Fundamentals
Unraveling the mystery of monads
Functional Programming Concepts for Imperative Programmers
JavaScript for ABAP Programmers - 7/7 Functional Programming
Plc part 3
Scala: functional programming for the imperative mind
Functional Programming in Swift
Functional Programming Fundamentals
The joy of functional programming
Functional programming in clojure
Functional Programming for Busy Object Oriented Programmers
Presentation of GetTogether on Functional Programming
Functional programming in Python 1st Edition David Mertz
Ad

More from Innovecs (20)

PDF
Building Efficient and High Performing iLottery Solutions
PDF
Innovecs Meetup Lifestory
PPTX
Подходы и технологии в React Redux
PPTX
Redux vs RxJS vs Mobx в связке с React
PDF
React & Redux (Lazarev)
PDF
Web Platform for Fashion Shop
PDF
Programmatic Advertising Platform
PDF
Multimedia Newsroom
PDF
Media Buying Platform (DSP+DPM)
PDF
Web-based Shipment Application
PDF
Digital Trading Platform
PDF
Mobile Insurance Agent
PDF
Online Learning Platform
PDF
Client Bank
PDF
Fertility Tracking App
PDF
Warranty Wallet App
PDF
Online Bingo Game
PDF
Secure Messenger
PDF
Search Data Platform
PDF
Website Builder for Insurance Agents
Building Efficient and High Performing iLottery Solutions
Innovecs Meetup Lifestory
Подходы и технологии в React Redux
Redux vs RxJS vs Mobx в связке с React
React & Redux (Lazarev)
Web Platform for Fashion Shop
Programmatic Advertising Platform
Multimedia Newsroom
Media Buying Platform (DSP+DPM)
Web-based Shipment Application
Digital Trading Platform
Mobile Insurance Agent
Online Learning Platform
Client Bank
Fertility Tracking App
Warranty Wallet App
Online Bingo Game
Secure Messenger
Search Data Platform
Website Builder for Insurance Agents

Recently uploaded (20)

PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Pharma ospi slides which help in ospi learning
PDF
01-Introduction-to-Information-Management.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
master seminar digital applications in india
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
Final Presentation General Medicine 03-08-2024.pptx
Anesthesia in Laparoscopic Surgery in India
O7-L3 Supply Chain Operations - ICLT Program
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chinmaya Tiranga quiz Grand Finale.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Presentation on HIE in infants and its manifestations
Pharma ospi slides which help in ospi learning
01-Introduction-to-Information-Management.pdf
GDM (1) (1).pptx small presentation for students
master seminar digital applications in india
Microbial disease of the cardiovascular and lymphatic systems
Module 4: Burden of Disease Tutorial Slides S2 2025
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet

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