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
Svetlin Nakov
Β 
PDF
Some basic FP concepts
Falko Riemenschneider
Β 
PPTX
Functional programming
Kibru Demeke
Β 
PDF
Python functional programming
Geison Goes
Β 
PPTX
Learn To Code: Introduction to c
SadhanaParameswaran
Β 
PPTX
Scala overview
dogstar
Β 
PDF
Functional Python Webinar from October 22nd, 2014
Reuven Lerner
Β 
PPTX
Es6 features part 1
Madhu Sudhan Subedi
Β 
Dev Concepts: Functional Programming
Svetlin Nakov
Β 
Some basic FP concepts
Falko Riemenschneider
Β 
Functional programming
Kibru Demeke
Β 
Python functional programming
Geison Goes
Β 
Learn To Code: Introduction to c
SadhanaParameswaran
Β 
Scala overview
dogstar
Β 
Functional Python Webinar from October 22nd, 2014
Reuven Lerner
Β 
Es6 features part 1
Madhu Sudhan Subedi
Β 

What's hot (19)

PDF
Functional programing in Javascript (lite intro)
Nikos Kalogridis
Β 
PDF
Functional JavaScript Fundamentals
Srdjan Strbanovic
Β 
PDF
Scala qq
ηΎ½η₯ˆ εΌ΅
Β 
PPTX
5.functions
Hardik gupta
Β 
PDF
Functional go
Geison Goes
Β 
PPT
ALGOL ailesi programlama dilleri
Cumhuriyet Üniversitesi
Β 
PDF
Introduction to functional programming
Konrad Szydlo
Β 
PPTX
Interpreter Design Pattern in Javascript
Dmytro Verbovyi
Β 
PPT
Scala functions
Knoldus Inc.
Β 
PDF
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
Β 
PDF
Lecture 3 getting_started_with__c_
eShikshak
Β 
PDF
Type Checking JavaScript
Pascal-Louis Perez
Β 
PPTX
An Introduction to Functional Programming with Javascript
Doug Sparling
Β 
PPTX
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
Β 
PPTX
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima
Β 
PDF
Functional programming in scala
Stratio
Β 
PPTX
Storage Class in C Progrmming
Kamal Acharya
Β 
PDF
Pointers & functions
Manjitsing Valvi
Β 
PPTX
Developer’s viewpoint on swift programming language
Azilen Technologies Pvt. Ltd.
Β 
Functional programing in Javascript (lite intro)
Nikos Kalogridis
Β 
Functional JavaScript Fundamentals
Srdjan Strbanovic
Β 
5.functions
Hardik gupta
Β 
Functional go
Geison Goes
Β 
ALGOL ailesi programlama dilleri
Cumhuriyet Üniversitesi
Β 
Introduction to functional programming
Konrad Szydlo
Β 
Interpreter Design Pattern in Javascript
Dmytro Verbovyi
Β 
Scala functions
Knoldus Inc.
Β 
Thinking in Functions: Functional Programming in Python
Anoop Thomas Mathew
Β 
Lecture 3 getting_started_with__c_
eShikshak
Β 
Type Checking JavaScript
Pascal-Louis Perez
Β 
An Introduction to Functional Programming with Javascript
Doug Sparling
Β 
Pointers,virtual functions and polymorphism cpp
rajshreemuthiah
Β 
WHY JAVASCRIPT FUNCTIONAL PROGRAMMING IS SO HARD?
reactima
Β 
Functional programming in scala
Stratio
Β 
Storage Class in C Progrmming
Kamal Acharya
Β 
Pointers & functions
Manjitsing Valvi
Β 
Developer’s viewpoint on swift programming language
Azilen Technologies Pvt. Ltd.
Β 
Ad

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

PDF
Ruby Functional Programming
Geison Goes
Β 
PDF
Go Beyond Higher Order Functions: A Journey into Functional Programming
Lex Sheehan
Β 
KEY
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
Β 
PDF
Functional programming in Scala
datamantra
Β 
ODP
Functional programming
S M Asaduzzaman
Β 
PDF
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
Β 
PDF
Real World Haskell: Lecture 1
Bryan O'Sullivan
Β 
PDF
(3) cpp procedural programming
Nico Ludwig
Β 
PPTX
DataWeave 2.0 Language Fundamentals
Joshua Erney
Β 
PPTX
PARADIGM IT.pptx
jamesmarken1
Β 
PDF
Programming in Scala - Lecture Two
Angelo Corsaro
Β 
KEY
LISP: How I Learned To Stop Worrying And Love Parantheses
Dominic Graefen
Β 
PPT
Programming Paradigms
Directi Group
Β 
PDF
Functional programming
ijcd
Β 
PDF
Functional Programming in JavaScript
Will Livengood
Β 
PPTX
About Functional Programming
Aapo KyrΓΆlΓ€
Β 
KEY
Functional programming in clojure
Juan-Manuel Gimeno
Β 
PDF
Dart workshop
Vishnu Suresh
Β 
PPTX
Functional reactive programming
Hung Hoang
Β 
PDF
pure-functional-programming.pdf
PuneetChaturvedi23
Β 
Ruby Functional Programming
Geison Goes
Β 
Go Beyond Higher Order Functions: A Journey into Functional Programming
Lex Sheehan
Β 
Scala: functional programming for the imperative mind
Sander Mak (@Sander_Mak)
Β 
Functional programming in Scala
datamantra
Β 
Functional programming
S M Asaduzzaman
Β 
Twins: Object Oriented Programming and Functional Programming
RichardWarburton
Β 
Real World Haskell: Lecture 1
Bryan O'Sullivan
Β 
(3) cpp procedural programming
Nico Ludwig
Β 
DataWeave 2.0 Language Fundamentals
Joshua Erney
Β 
PARADIGM IT.pptx
jamesmarken1
Β 
Programming in Scala - Lecture Two
Angelo Corsaro
Β 
LISP: How I Learned To Stop Worrying And Love Parantheses
Dominic Graefen
Β 
Programming Paradigms
Directi Group
Β 
Functional programming
ijcd
Β 
Functional Programming in JavaScript
Will Livengood
Β 
About Functional Programming
Aapo KyrΓΆlΓ€
Β 
Functional programming in clojure
Juan-Manuel Gimeno
Β 
Dart workshop
Vishnu Suresh
Β 
Functional reactive programming
Hung Hoang
Β 
pure-functional-programming.pdf
PuneetChaturvedi23
Β 
Ad

More from Innovecs (20)

PDF
Building Efficient and High Performing iLottery Solutions
Innovecs
Β 
PDF
Innovecs Meetup Lifestory
Innovecs
Β 
PPTX
ΠŸΠΎΠ΄Ρ…ΠΎΠ΄Ρ‹ ΠΈ Ρ‚Π΅Ρ…Π½ΠΎΠ»ΠΎΠ³ΠΈΠΈ Π² React Redux
Innovecs
Β 
PPTX
Redux vs RxJS vs Mobx в связкС с React
Innovecs
Β 
PDF
React & Redux (Lazarev)
Innovecs
Β 
PDF
Web Platform for Fashion Shop
Innovecs
Β 
PDF
Programmatic Advertising Platform
Innovecs
Β 
PDF
Multimedia Newsroom
Innovecs
Β 
PDF
Media Buying Platform (DSP+DPM)
Innovecs
Β 
PDF
Web-based Shipment Application
Innovecs
Β 
PDF
Digital Trading Platform
Innovecs
Β 
PDF
Mobile Insurance Agent
Innovecs
Β 
PDF
Online Learning Platform
Innovecs
Β 
PDF
Client Bank
Innovecs
Β 
PDF
Fertility Tracking App
Innovecs
Β 
PDF
Warranty Wallet App
Innovecs
Β 
PDF
Online Bingo Game
Innovecs
Β 
PDF
Secure Messenger
Innovecs
Β 
PDF
Search Data Platform
Innovecs
Β 
PDF
Website Builder for Insurance Agents
Innovecs
Β 
Building Efficient and High Performing iLottery Solutions
Innovecs
Β 
Innovecs Meetup Lifestory
Innovecs
Β 
ΠŸΠΎΠ΄Ρ…ΠΎΠ΄Ρ‹ ΠΈ Ρ‚Π΅Ρ…Π½ΠΎΠ»ΠΎΠ³ΠΈΠΈ Π² React Redux
Innovecs
Β 
Redux vs RxJS vs Mobx в связкС с React
Innovecs
Β 
React & Redux (Lazarev)
Innovecs
Β 
Web Platform for Fashion Shop
Innovecs
Β 
Programmatic Advertising Platform
Innovecs
Β 
Multimedia Newsroom
Innovecs
Β 
Media Buying Platform (DSP+DPM)
Innovecs
Β 
Web-based Shipment Application
Innovecs
Β 
Digital Trading Platform
Innovecs
Β 
Mobile Insurance Agent
Innovecs
Β 
Online Learning Platform
Innovecs
Β 
Client Bank
Innovecs
Β 
Fertility Tracking App
Innovecs
Β 
Warranty Wallet App
Innovecs
Β 
Online Bingo Game
Innovecs
Β 
Secure Messenger
Innovecs
Β 
Search Data Platform
Innovecs
Β 
Website Builder for Insurance Agents
Innovecs
Β 

Recently uploaded (20)

PDF
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
Β 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
Β 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
Β 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
Β 
PDF
High Ground Student Revision Booklet Preview
jpinnuck
Β 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
Β 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
Β 
PDF
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
Β 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
Β 
PPTX
Introduction and Scope of Bichemistry.pptx
shantiyogi
Β 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
Β 
PDF
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
Β 
PDF
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
Β 
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
Β 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
Β 
PPTX
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
Β 
PPTX
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
Β 
PPTX
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
Β 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
Β 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
Β 
1.Natural-Resources-and-Their-Use.ppt pdf /8th class social science Exploring...
Sandeep Swamy
Β 
The Picture of Dorian Gray summary and depiction
opaliyahemel
Β 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
Β 
Arihant Class 10 All in One Maths full pdf
sajal kumar
Β 
High Ground Student Revision Booklet Preview
jpinnuck
Β 
Congenital Hypothyroidism pptx
AneetaSharma15
Β 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
Β 
Module 3: Health Systems Tutorial Slides S2 2025
Jonathan Hallett
Β 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
Β 
Introduction and Scope of Bichemistry.pptx
shantiyogi
Β 
Types of Literary Text: Poetry and Prose
kaelandreabibit
Β 
3.The-Rise-of-the-Marathas.pdfppt/pdf/8th class social science Exploring Soci...
Sandeep Swamy
Β 
Phylum Arthropoda: Characteristics and Classification, Entomology Lecture
Miraj Khan
Β 
Open Quiz Monsoon Mind Game Final Set.pptx
Sourav Kr Podder
Β 
Strengthening open access through collaboration: building connections with OP...
Jisc
Β 
Dakar Framework Education For All- 2000(Act)
santoshmohalik1
Β 
Open Quiz Monsoon Mind Game Prelims.pptx
Sourav Kr Podder
Β 
Skill Development Program For Physiotherapy Students by SRY.pptx
Prof.Dr.Y.SHANTHOSHRAJA MPT Orthopedic., MSc Microbiology
Β 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
Β 
How to Manage Global Discount in Odoo 18 POS
Celine George
Β 

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!