The Python Hitchhiker’s Guide to Haskell

The Python Hitchhiker’s Guide to Haskell

Howard B. Golden

June 17, 2010

HHG

HHG

Outline

What is Haskell?

“Haskell is a general purpose, purely functional programming language incorporating many recent innovations in programming language design.”

Haskell 98 Report

Here, “pure” means that the language has no side-effects. How this is possible will be explained shortly.

Haskell’s features

Haskell provides

More Haskell features

Haskell also has a rich set of primitive data types including

Some Simple Example Programs

Of course, we begin with “Hello, World!”

main = putStrLn "Hello, World!"

Simple interaction

main = do putStrLn "What is 2 + 2?"
x <- readLn
if x == 4
then putStrLn "You're right!"
else putStrLn "You're wrong!"

Some Simple Example Programs

module Main where

factorial n = if n == 0
then 1
else n * factorial (n - 1)

main = do putStrLn "What is 5! ?"
x <- readLn
if x == factorial 5
then putStrLn "You're right!"
else putStrLn "You're wrong!"

Function signature and definition

factorial :: Integer -> Integer
factorial n = if n == 0
then 1
else n * factorial (n - 1)

Haskell vs. Python

CharacteristicHaskellPython
ParadigmFunctionalImperative
Object-oriented?No - uses type classes insteadYes
Data typesStaticDynamic
EvaluationLazyStrict
PolymorphismYesDuck-typing

Data Types and Type Classes

Haskell allows the programmer to define new data types. However, Haskell is not object-oriented. Instead it provides type classes which enable many of the same features without the strict hierarchy of objects.

Examples of type definitions:

data  Bool  =  False | True deriving 
(Read, Show, Eq,
Ord, Enum, Bounded)
data  [a]  =  [] | a : [a]  deriving (Eq, Ord)
type  String  =  [Char]

Type class example

Example of type class definition:

The Eq type class is used by almost all types.

class Eq a  where
(==), (/=) :: a -> a -> Bool

x /= y = not (x == y)
x == y = not (x /= y)

It is only necessary to define one of == and /=. The other definition can be inferred by negation.

Standard Haskell type classes

Haskell classes

Haskell classes

Is Haskell worth learning?

“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, “How to Become a Hacker”

The same can be said for Haskell.

How hard is it to learn Haskell?

Haskell is harder to learn than Python or most other languages.

However, the main reason that Haskell is harder to learn is that there is so much more power to it than most other languages.

What are the difficult areas?

Haskell learners often have difficulty with

Is Haskell ready for the real world?

Yes!

What resources are available to help me?

Questions?

Any questions?