SlideShare uma empresa Scribd logo
1 de 27
Baixar para ler offline
Functional
Programming
— Alcides Fonseca
with and without Haskell
Functional Programming
ˈfəŋ(k)-shnəl prō-ˌgrammiŋ
The art of writing programs that function as expected.
noun.
int ageOf(Person p);
int ageOf(Person p) {
return today() - birthdate[d];
}
double :: Int -> Int
double x = x + x
times :: Int -> Int -> Int
times 0 _ = 0
times 1 x = x
times n x = x + (times (n-1) x)
double = times 2
double :: Int -> Int
times 1 n = n
times a b = a + (times (b-1) a)
power 1 n = n
power a b = a * (power (b-1) a)
recursiveOp :: (Int -> Int -> Int) -> Int -> Int -> Int
recursiveOp op a 1 = a
recursiveOp op a b = op a (self a (b-1))
where self = recursiveOp op
times = recursiveOp (+)
double = times 2
recursiveOp :: Num t => (t -> t -> t) -> t -> Int -> t
recursiveOp op a 1 = a
recursiveOp op a b = op a (self a (b-1))
where self = recursiveOp op
ones :: [Int]
ones = 1:ones
doubleList :: [Int] -> [Int]
doubleList [] = []
doubleList (d:ds) = (double d):(doubleList ds)
:
1 :
2 :
3 :
4 []
applyOverList :: (Int -> Int) -> [Int] -> [Int]
applyOverList f xs = [ f x | x <- xs ]
map :: (a -> b) -> [a] -> [b]
foldr :: (a -> b -> b) -> b -> [a] -> b
quicksort :: [Int] -> [Int]
quicksort [] = []
quicksort (x:xs) = (quicksort (filter (<= x) xs)) ++ [x]
++ (quicksort (filter (> x) xs))
main :: IO ()
main = do
putStr "double: "
putStrLn (show (double 3))
main :: (RealWorld ->) ((),RealWorld)
main = return a
where a = putStr "double: "
>>= putStrLn (show (double 3))
(>>=) :: IO a -> (a -> IO b) -> IO b
return :: a -> IO a
Resumo
The Good
• Modularity of Patterns
(1001 classes = 3 lines of code)
• Signatures express all effects
(longer, but makes dependencies and bugs easier to find)
• Speeds in the order of magnitude of C
• Hackage, Cabal & Hoogle
The Great
• loc(f) <= 5
• If it compiles, it will most surely work
• Free parallelization
• Can be proved correct
The Bad
“Lisp is worth learning for the profound enlightenment experience
you will have when you finally get it;
That experience will make you a better programmer for the rest of
your days, even if you never actually use Lisp itself a lot.”
— Eric Raymond
“Ditto for Haskell.”
— Alcides Fonseca
Learn Haskell, so you can improve at X
trips = [ (from_, to) for from_ in cities for to in cities if not to == from_ ]
def parseArgument(arg: String):String = arg match {
case "-h" | "--help" => displayHelp
case "-v" | "--version" => displayVersion
case _ => "RTFM"
}
double integral(double (*f)(double x), double a, double b);
var files = from file in DirInfo.EnumerateFiles()
where file.CreationTimeUtc > dt1 &
file.CreationTimeUtc < dt2
select file;
Thank you
me@alcidesfonseca.com
https://github.com/alcides/haskell_tutorial
— Alcides

Mais conteúdo relacionado

Destaque

Destaque (6)

Workshop Git
Workshop GitWorkshop Git
Workshop Git
 
Programming Paradigms
Programming ParadigmsProgramming Paradigms
Programming Paradigms
 
My Web20
My Web20My Web20
My Web20
 
Web202010 sagamore
Web202010 sagamoreWeb202010 sagamore
Web202010 sagamore
 
A deep look at Social Media
A deep look at Social MediaA deep look at Social Media
A deep look at Social Media
 
Introdução Web
Introdução WebIntrodução Web
Introdução Web
 

Último

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburgmasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 

Último (20)

%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 

Functional Programming with and without Haskell

  • 2. Functional Programming ˈfəŋ(k)-shnəl prō-ˌgrammiŋ The art of writing programs that function as expected. noun.
  • 3.
  • 4.
  • 6. int ageOf(Person p) { return today() - birthdate[d]; }
  • 7.
  • 8.
  • 9. double :: Int -> Int double x = x + x
  • 10. times :: Int -> Int -> Int times 0 _ = 0 times 1 x = x times n x = x + (times (n-1) x) double = times 2 double :: Int -> Int
  • 11.
  • 12. times 1 n = n times a b = a + (times (b-1) a) power 1 n = n power a b = a * (power (b-1) a) recursiveOp :: (Int -> Int -> Int) -> Int -> Int -> Int recursiveOp op a 1 = a recursiveOp op a b = op a (self a (b-1)) where self = recursiveOp op times = recursiveOp (+) double = times 2
  • 13. recursiveOp :: Num t => (t -> t -> t) -> t -> Int -> t recursiveOp op a 1 = a recursiveOp op a b = op a (self a (b-1)) where self = recursiveOp op
  • 14.
  • 15. ones :: [Int] ones = 1:ones
  • 16. doubleList :: [Int] -> [Int] doubleList [] = [] doubleList (d:ds) = (double d):(doubleList ds) : 1 : 2 : 3 : 4 [] applyOverList :: (Int -> Int) -> [Int] -> [Int] applyOverList f xs = [ f x | x <- xs ] map :: (a -> b) -> [a] -> [b] foldr :: (a -> b -> b) -> b -> [a] -> b
  • 17.
  • 18. quicksort :: [Int] -> [Int] quicksort [] = [] quicksort (x:xs) = (quicksort (filter (<= x) xs)) ++ [x] ++ (quicksort (filter (> x) xs))
  • 19.
  • 20. main :: IO () main = do putStr "double: " putStrLn (show (double 3)) main :: (RealWorld ->) ((),RealWorld) main = return a where a = putStr "double: " >>= putStrLn (show (double 3)) (>>=) :: IO a -> (a -> IO b) -> IO b return :: a -> IO a
  • 22. The Good • Modularity of Patterns (1001 classes = 3 lines of code) • Signatures express all effects (longer, but makes dependencies and bugs easier to find) • Speeds in the order of magnitude of C • Hackage, Cabal & Hoogle
  • 23. The Great • loc(f) <= 5 • If it compiles, it will most surely work • Free parallelization • Can be proved correct
  • 25. “Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; That experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot.” — Eric Raymond “Ditto for Haskell.” — Alcides Fonseca
  • 26. Learn Haskell, so you can improve at X trips = [ (from_, to) for from_ in cities for to in cities if not to == from_ ] def parseArgument(arg: String):String = arg match { case "-h" | "--help" => displayHelp case "-v" | "--version" => displayVersion case _ => "RTFM" } double integral(double (*f)(double x), double a, double b); var files = from file in DirInfo.EnumerateFiles() where file.CreationTimeUtc > dt1 & file.CreationTimeUtc < dt2 select file;