Haskell introduction for Ruby programmers
Haskell Emacs Lisp C Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Haskell: The Easy Stuff

Topics and Goals

  1. The primary properties of Ruby and Haskell
  2. Learn the basics of Haskell (functions and types)
  3. Haskell lessons for writing better Ruby code

What is Ruby?

  1. Imperative and object-oriented
  2. Objects are first-class citizens
  3. Nearly everything is mutable
  4. Dynamically typed with a class-based type system
  5. Strict evaluation
  6. Interpreted

What is Haskell?

  1. Declarative and purely functional
  2. Functions are first-class citizens
  3. Functions are not allowed to have side effects (no state)
  4. Statically typed with a strong type system
  5. Non-strict evaluation (lazy evaluation)
  6. Compiled (native object code) or interpreted

Haskell Basics


  • Braces and semicolons or
  • Layout (use indentation like Python)

Defining Functions

  • Name and arguments go on the left-hand side of the equals sign
  • The body of the function goes on the right-hand side
  • Functions must produce the same result given the same arguments
  • The only “data” a function can see are its arguments
  • The compiler can infer type information automatically
  • You can also manually specify type information
  • Giving explicit type information gives the compiler something to check against (what you expect vs. is really given)

Function Types and Type Classes

  • When you give type information you can use:
    • Specific types (e.g. Int, Bool)
    • Type variables (e.g. a)
    • Type classes (e.g. Num a =>)
    • That is, functions can be polymorphic

User Defined Types

  • Some types to review
    1. Bool
    2. Person (user-defined type in types.hs)
    3. Maybe
  • Difference between type constructors and value constructors
  • Pattern matching and value constructors

Some Real World Haskell

Why I Like Haskell

  • Everything is very well thought out
  • The syntax is beautiful and clean
  • The type system is fantastic (type-driven development)
  • Concurrency is a high-priority
  • Function purity makes it easier to reason/understand
  • The best of both worlds (interpreted vs. compiled)

Writing Better Ruby

  • Minimize side effects
  • Use fewer instance variables
  • Write simpler classes with fewer responsibilities
  • Use nil less often?

Learning Haskell

Recommended Way to Learn Haskell

  1. Learn You a Haskell (buy the book)
  2. Typeclassopedia (dig into Haskell types)
  3. Why Do Monads Matter? (great article)
  4. All About Monads (digs into each of the standard monads)
  5. IO inside (understand IO and side effects)
  6. Review Real World Haskell (the online version, watch for typos)
  7. Another checklist covering how to learn Haskell

Things to Keep In Mind

  • If Haskell is your first purely functional language it might take some time to get out of your imperative skin
  • Haskell is based on abstract mathematical concepts such as category theory so expect to spend some time reviewing math texts

Getting Help