SlideShare a Scribd company logo
1 of 12
Download to read offline
Funkcionális
programozás (FP)
kedvcsináló
Imperatív programozási
stílus
• Neumann elv
• Változók, ciklusok, elágazások
• Pascal, C,…
• “Bolyongás az állapottérben”
Deklaratív programozási
stílus
• Nem hogyan, hanem mit
• Többek közt az FP-k is
Imperativ (C) FP (Haskell)
int fact(int n){

int f=1; 

while(n>1)

f*=n--; 

return f;

fact 0 = 1 

fact n = n * fact n-1
Prímszámkeresés I.
deklaratív módon
Haskell-ben programozva:
osztok :: Int-> [Int]
osztok n =[x | x<-[1..n],n `mod` x == 0]
primek n =[x | x<-[1..n],osztok x == [1,x]]
Funkcionális programozási
nyelvek
• Minden program egy függvény.
• A függvények first class citizen-k, azaz a függvények
használhatók bárhol, bármire a programban
• Magasabb rendű függvények: filter, map, foldr
• Lambda kifejezés
• Nincs változó, nincs sírás
• Rekurzió
FP nyelvek
• 1959 Lisp
• 1975-77 ML, FP, Scheme
• 1978 Smalltalk
• 1986 Standard ML
• 1990 Haskell, Erlang
• 1999  XSLT
• 2000  OCaml
• 2003 Scala, XQuery
• 2005 F#
• 2007 Clojure
• 2014 Swift
FP miért érdekes
• Divatos
• Könnyebb leírni bonyolult mintákat. pl async
hívások, exception-k
• Nincs változó => könnyen párhuzamosítható
• Egyre több nyelvben bukkan fel

Java8, JavaScript
• Nem ellentette az OO-nak. Lásd Scala
Parciális függvények
add x y = x +y //Int->Int->Int

add5 y = add 5 ??????
Map, filter, foldr
x=[1..10]
x1=map (x ->x+5) x
x2=filter (x ->x>5) x
szum :: [Integer] -> Integer
szum [] = 0
szum (x:xs) =x+szum xs
prod :: [Integer] -> Integer
prod [] = 1
prod (x:xs) =x*prod xs
//Csak két karakterben térnek el. Legyen a fentiek általánosítása a foldr fv.
prod2= foldr (*) 1
szum2 = foldr (+) 0
QuickSort megvalósítás
Ha valaki nem ismeri az algoritmust, a programkódból
megtanulhatja.
qsort :: [Int] -> [Int]
qsort [] = []
qsort(x:xs)=
qsort smaller ++ [x] ++ qsort larger
where
smaller = [a|a<-xs, a<=x]
larger = [b|b<-xs, b>=x]
Prímszám II.
Érdekességként, egy haladó szintű példa.
Eratoszthenész szitájának Scala megvalósítása:
def from(n: Int): Stream[Int] = n #:: from(n+1) from: from[](val n: Int) => Stream[Int]

(from(2) take 20).toList res2: List[Int] = List(2, 3, 4, 5, 6, 7, 8,
9,



def sieve(s: Stream[Int]): Stream[Int] = sieve:sieve[](val s:Stream[Int])=>
Stream[Int]

s.head #:: sieve(s.tail filter (_ % s.head != 0))

val primes = sieve(from(2)) primes: Stream[Int] = Stream(2, ?)



(primes take 20).toList res3: List[Int] = List(2, 3, 5, 7, 11, 13,
Irodalom
• Imperatív programozás matematikai alapjai:

Fóti - Horváth: Bevezetés a programozásba

http://www.inf.elte.hu/ekonyvtar/bevprog.pdf
• John Hughes: Why Functional Programming
Matters? (Addison-Wesley:1990)
• coursera.org: Programming Languages

Functional Programming Principles
• edx.org: FP101x Introduction to FP

More Related Content

Featured

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 

Featured (20)

Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 

Funkcionális programozás ízelítő

  • 2. Imperatív programozási stílus • Neumann elv • Változók, ciklusok, elágazások • Pascal, C,… • “Bolyongás az állapottérben”
  • 3. Deklaratív programozási stílus • Nem hogyan, hanem mit • Többek közt az FP-k is Imperativ (C) FP (Haskell) int fact(int n){
 int f=1; 
 while(n>1)
 f*=n--; 
 return f;
 fact 0 = 1 
 fact n = n * fact n-1
  • 4. Prímszámkeresés I. deklaratív módon Haskell-ben programozva: osztok :: Int-> [Int] osztok n =[x | x<-[1..n],n `mod` x == 0] primek n =[x | x<-[1..n],osztok x == [1,x]]
  • 5. Funkcionális programozási nyelvek • Minden program egy függvény. • A függvények first class citizen-k, azaz a függvények használhatók bárhol, bármire a programban • Magasabb rendű függvények: filter, map, foldr • Lambda kifejezés • Nincs változó, nincs sírás • Rekurzió
  • 6. FP nyelvek • 1959 Lisp • 1975-77 ML, FP, Scheme • 1978 Smalltalk • 1986 Standard ML • 1990 Haskell, Erlang • 1999  XSLT • 2000  OCaml • 2003 Scala, XQuery • 2005 F# • 2007 Clojure • 2014 Swift
  • 7. FP miért érdekes • Divatos • Könnyebb leírni bonyolult mintákat. pl async hívások, exception-k • Nincs változó => könnyen párhuzamosítható • Egyre több nyelvben bukkan fel
 Java8, JavaScript • Nem ellentette az OO-nak. Lásd Scala
  • 8. Parciális függvények add x y = x +y //Int->Int->Int
 add5 y = add 5 ??????
  • 9. Map, filter, foldr x=[1..10] x1=map (x ->x+5) x x2=filter (x ->x>5) x szum :: [Integer] -> Integer szum [] = 0 szum (x:xs) =x+szum xs prod :: [Integer] -> Integer prod [] = 1 prod (x:xs) =x*prod xs //Csak két karakterben térnek el. Legyen a fentiek általánosítása a foldr fv. prod2= foldr (*) 1 szum2 = foldr (+) 0
  • 10. QuickSort megvalósítás Ha valaki nem ismeri az algoritmust, a programkódból megtanulhatja. qsort :: [Int] -> [Int] qsort [] = [] qsort(x:xs)= qsort smaller ++ [x] ++ qsort larger where smaller = [a|a<-xs, a<=x] larger = [b|b<-xs, b>=x]
  • 11. Prímszám II. Érdekességként, egy haladó szintű példa. Eratoszthenész szitájának Scala megvalósítása: def from(n: Int): Stream[Int] = n #:: from(n+1) from: from[](val n: Int) => Stream[Int]
 (from(2) take 20).toList res2: List[Int] = List(2, 3, 4, 5, 6, 7, 8, 9,
 
 def sieve(s: Stream[Int]): Stream[Int] = sieve:sieve[](val s:Stream[Int])=> Stream[Int]
 s.head #:: sieve(s.tail filter (_ % s.head != 0))
 val primes = sieve(from(2)) primes: Stream[Int] = Stream(2, ?)
 
 (primes take 20).toList res3: List[Int] = List(2, 3, 5, 7, 11, 13,
  • 12. Irodalom • Imperatív programozás matematikai alapjai:
 Fóti - Horváth: Bevezetés a programozásba
 http://www.inf.elte.hu/ekonyvtar/bevprog.pdf • John Hughes: Why Functional Programming Matters? (Addison-Wesley:1990) • coursera.org: Programming Languages
 Functional Programming Principles • edx.org: FP101x Introduction to FP