Functional Programming with and without Haskell
-
Upload
alcides-fonseca -
Category
Software
-
view
270 -
download
2
Transcript of Functional Programming with and without Haskell
![Page 1: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/1.jpg)
Functional Programming
— Alcides Fonseca
with and without Haskell
![Page 2: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/2.jpg)
Functional Programming\ˈfəŋ(k)-shnəl prō-ˌgrammiŋ\
The art of writing programs that function as expected.noun.
![Page 3: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/3.jpg)
![Page 4: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/4.jpg)
![Page 5: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/5.jpg)
int ageOf(Person p);
![Page 6: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/6.jpg)
int ageOf(Person p) {return today() - birthdate[d];
}
![Page 7: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/7.jpg)
![Page 8: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/8.jpg)
![Page 9: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/9.jpg)
double :: Int -> Intdouble x = x + x
![Page 10: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/10.jpg)
times :: Int -> Int -> Inttimes 0 _ = 0times 1 x = xtimes n x = x + (times (n-1) x)
double = times 2double :: Int -> Int
![Page 11: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/11.jpg)
![Page 12: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/12.jpg)
times 1 n = ntimes a b = a + (times (b-1) a)
power 1 n = npower a b = a * (power (b-1) a)
recursiveOp :: (Int -> Int -> Int) -> Int -> Int -> IntrecursiveOp op a 1 = arecursiveOp op a b = op a (self a (b-1))
where self = recursiveOp op
times = recursiveOp (+)double = times 2
![Page 13: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/13.jpg)
recursiveOp :: Num t => (t -> t -> t) -> t -> Int -> trecursiveOp op a 1 = arecursiveOp op a b = op a (self a (b-1))
where self = recursiveOp op
![Page 14: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/14.jpg)
![Page 15: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/15.jpg)
ones :: [Int]ones = 1:ones
![Page 16: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/16.jpg)
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
![Page 17: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/17.jpg)
![Page 18: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/18.jpg)
quicksort :: [Int] -> [Int]quicksort [] = []quicksort (x:xs) = (quicksort (filter (<= x) xs)) ++ [x] ++ (quicksort (filter (> x) xs))
![Page 19: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/19.jpg)
![Page 20: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/20.jpg)
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
![Page 21: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/21.jpg)
Resumo
![Page 22: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/22.jpg)
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
![Page 23: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/23.jpg)
The Great
• loc(f) <= 5
• If it compiles, it will most surely work
• Free parallelization
• Can be proved correct
![Page 24: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/24.jpg)
The Bad
![Page 25: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/25.jpg)
“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
![Page 26: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/26.jpg)
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;
![Page 27: Functional Programming with and without Haskell](https://reader031.fdocuments.net/reader031/viewer/2022021507/58f2fe141a28ab6c6e8b4577/html5/thumbnails/27.jpg)
Thank you
https://github.com/alcides/haskell_tutorial
— Alcides