Why Use Haskell?

3,913 views
3,645 views

Published on

A few times now, I've been asked, "Haskell looks cool, but what should I use it for?" This presentation attempts to showcase some of the unique parts of Haskell in order to make it easier to understand why you might be interested in learning it.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,913
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
35
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Why Use Haskell?

  1. 1. Why use Haskell?
  2. 2. “ Haskell was made by some really smart guys (with PhDs). - Learn You a Haskell
  3. 3. Haskell is a purely functional programming language.
  4. 4. Easier to Understand
  5. 5. Ruby def plan_name @user.account.plan.name end Haskell planName :: User -> String planName = name . plan . account
  6. 6. Ruby def slugs(projects) projects. map { |project| project. name. downcase }. uniq end Haskell slugs :: [Project] -> [String] slugs = uniq . map (downcase . projectName)
  7. 7. Less Buggy • No distinction between pass by reference, pass by value. • Don’t need to worry about dup, freeze, and friends.
  8. 8. Easier to Write
  9. 9. Ruby Haskell def combine(elements, new_element) combine :: Combinable a => Seq a -> existing_index = a -> elements. Seq a find_index { |existing| combine xs x = existing. case existingIndex of combinable?(new_element) Just i -> } adjust (combine x) i xs if existing_index Nothing -> existing_element = x <| xs elements. where existingIndex = delete_at(existing_index) findIndexL (similar x) xs elements << existing_element. merge(new_element) else elements << new_element end end
  10. 10. Paralellization • Ruby: Thread, Mutex, Semaphore, Locking, Contention • Haskell: par, pseq
  11. 11. Haskell is lazy.
  12. 12. Clearer Code
  13. 13. Ruby Haskell def letter_values(start) letters = 'a'..'z' letters. zip(start..(letters.count)) end letterValues :: Int -> [(Char, Int)] letterValues start = zip ['a'..'z'] [start..] ! main :: IO () main = do (start:_) <- getArgs mapM_ print $ letterValues $ read start def main start = ARGV[0].to_i puts letter_values(start). map(&:inspect) end !
  14. 14. Ruby def letter_values(start) ('a'..'z'). each_with_index do |letter, i| [letter, i + start] end end ! def main start = ARGV[0].to_i puts letter_values(start). map(&:inspect) end Haskell letterValues :: Int -> [(Char, Int)] letterValues start = zip ['a'..'z'] [start..] ! main :: IO () main = do (start:_) <- getArgs mapM_ print $ letterValues $ read start
  15. 15. Better Memory Usage
  16. 16. Ruby def words_per_line(string) string. split("n"). map { |line| line. split(' '). size }. average end ! def main puts words_per_line(STDIN.read) end Haskell wordsPerLine :: String -> Float wordsPerLine = mean . map (length . words) . lines ! main :: IO () main = print . wordsPerLine =<< getContents
  17. 17. Ruby def words_per_line(io) io. each_line. map { |line| line. split(' '). size }. average end ! def main puts words_per_line(STDIN) end Haskell wordsPerLine :: String -> Float wordsPerLine = mean . map (length . words) . lines ! main :: IO () main = print . wordsPerLine =<< getContents
  18. 18. Haskell is statically typed.
  19. 19. Clarity
  20. 20. Prelude> :type elemsWithIndex elemsWithIndex :: [b] -> [(b, Int)]
  21. 21. Speed
  22. 22. Type Safety
  23. 23. Ruby class Square def initialize(length) @length = length end ! def area @length * @length end end ! class Rectangle def initialize(width, length) @width, @length = width, length end ! def area @width * @length end end Haskell class Shape a where area :: a -> Int ! data Square = Square Int instance Shape Square where area (Square length) = length * length ! data Rectangle = Rectangle Int Int instance Shape Rectangle where area (Rectangle width length) = width * length
  24. 24. Ruby def plan_name(user) if user.account user.account.plan.name end end Haskell planName :: User -> String planName user = (name . plan) <$> account user
  25. 25. Ruby def plan_name(user) user.account.plan.name end Airbrake Error Haskell planName :: User -> String planName user = name $ plan $ account user Compile Error
  26. 26. Haskell is fun, safe, and fast. • Clearer code with types and less boilerplate. • Fewer bugs with type safety and immutable objects. • Compiled code runs quickly, even in parallel.
  27. 27. Getting Started brew install haskell-platform Learn You a Haskell http://learnyouahaskell.com

×