# Developing in Haskell - Presentation --- ![](https://www.fpcomplete.com/hubfs/haskell_logo.svg =120x) ## Developing in Haskell Dmitriy Dubson, Labs Engineer --- Motivation: - Completely different from my day-to-day in the world of "imperative" languages - What can I learn from this language? - Can I be productive in this language? - More and more functional concepts creeping into mainstream languages. - I have much time, and ~~nowhere~~ **can't** go anywhere. --- 🎉 Haskell turns 30! 🎉 Initial release in 1990. --- Haskell is a purely functional programming language. Haskell is **not** just for research. It's a general-purpose language and production-ready --- Fighting spam at Facebook : "The Haskell-powered Sigma now runs in production, serving more than one million requests per second." $^1$ --- ## Concept Pure functions - There are no interactions with the outside world (side effects) ```haskell= add :: Int -> Int -> Int add x y = x + y ``` is mathematically equivalent: $$ f(x,y) = x+y $$ --- How do I solve problems with only stateless, pure functions? --- ## Concept Function Composition `.` (dot) operator strings functions in a chain Output of one function is input to another ```haskell= sentence = "The quick brown fox jumps over the lazy dog" isOddNumberOfWords = odd . length . words $ sentence ``` --- How do I interact with the user or any other system if everything is pure? --- ## Concept (IO) Actions are for side effects ```haskell= readUserInputFromCommandLine :: IO String readUserInputFromCommandLine = do userInput <- getContents return userInput ``` --- ## Concept Currying and Partial Application --- ## Concept Lazy evaluation ```haskell= add :: Int -> Int -> Int add x y = x + y twoPlusTwo = add 2 2 finalResult = add twoPlusTwo 5 -- -> 9 ``` ```haskell= infiniteList :: [Int] infiniteList = [0..] firstFive = take 5 infiniteList -- > [0,1,2,3,4] ``` --- ## What I didn't mention - Typeclasses - Recursive data structures - Algebraic data types - Monads, monads, monads --- ## Toolchain *GHC* - Glasgow Haskell Compiler $^3$ *stack* - build and lifecycle manager Available on MacOS, Windows, ```bash stack new my-project && cd my-project stack run stack test stack build ``` --- ## Testing *HSpec* - based on RSpec ```haskell= describe "when provided two integers" $ do it "adds them correctly" $ do (add 5 5) `shouldBe` 10 ``` *QuickCheck* - random input generator ```haskell= describe "when provided two integers" $ do it "adds them correctly" $ property $ \randomInput -> (add 5 randomInput) `shouldBe` (5 + randomInput) ``` --- ## Takeaways - The language forces you to think about your problem as a series of tiny problems - It's worth learning (and other purely functional languages) - You'll think about making your code safe and testable everywhere you go --- # Thanks! 🎉 --- ## References and Further Reading (1) https://engineering.fb.com/security/fighting-spam-with-haskell/ (2) http://www.stephendiehl.com/posts/production.html (3) https://www.haskell.org/ghc/
{"metaMigratedAt":"2023-06-15T05:38:29.112Z","metaMigratedFrom":"Content","title":"Developing in Haskell - Presentation","breaks":true,"contributors":"[{\"id\":\"6221bbf6-10a0-4687-9087-8c2420c49c42\",\"add\":3408,\"del\":265}]"}
    297 views