Cover image for Relearn You a Haskell (Part 2: List Comprehensions, Tuples, and Types)

Relearn You a Haskell (Part 2: List Comprehensions, Tuples, and Types)

awwsmm profile image Andrew (he/him) ・8 min read

This is a continuation of my series of quick blog posts about Haskell. It's meant as a refresher for Haskell syntax and features for someone who maybe learned a bit of Haskell a while ago but who hasn't used it much and has forgotten most of what they learned. It's basically a quick summary of Learn You a Haskell, which is a super awesome book that you should definitely go spend money on.

(Note: There are lots more resources available that aren't LYAH. For instance, check out this paper written by three of the designers of Haskell in 1999.)

list comprehensions

List comprehensions have an output function, one or more input sets, and one or more predicates, in that order. A basic list comprehension looks like:

ghci> [ <output function> | <input set>, ..., <predicate>, ... ]

The input set is a list of values which are fed, in order, to the output function. Ultimately, the generated (output) list will consist of all of the values of the input set, which, once fed through the output function, satisfy the predicate. For example:

ghci> [ x*x | x <- [1..10], mod x 2 == 0 ]

The above prints the square of all values x, where x is drawn from the set [1..10], provided that mod x 2 is equal to 0. Another way of looking at it is that we first take the list of all numbers [1..10] and filter them through the predicate (mod x 2 == 0 means we only take the even numbers 2, 4, 6, 8, 10) and then square those numbers (so we end up with 4, 16, 36, 64, 100).

A list comprehension with multiple input sets will loop over every possible pair (or triple, or 4-tuple, ...) from the given sets and a comprehension with multiple predicates will only return values which satisfy all of the predicates. For instance:

ghci> [ (x,y) | x <- [1..3], y <- [4..6] ]
ghci> take 10 [ x | x <- [1..], x > 10, x /= 21, odd x ]

Here are some fun, but simple(-ish), list comprehensions:


A classic programming interview question.

ghci> [ if (x `mod` 15 == 0) then "FizzBuzz" else if (x `mod` 3 == 0) then "Fizz" else if (x `mod` 5 == 0) then "Buzz" else show x | x <- [1..100] ]

Sieve of Eratosthenes

This list comprehension generates prime numbers.

ghci> take 10 [ round x | x <- [2..], let l = [2..(ceiling(sqrt(x)))], all (/=0) (map (mod (round x)) l) ]

Fibonacci Numbers

Uses the golden ratio to generate the Fibonacci sequence.

ghci> let phi = ((1.0 + sqrt 5.0) / 2.0) in take 20 [ round (phi**x / (sqrt 5.0)) | x <- [1..] ]

...remember that strings in Haskell are just lists of characters, so you can perform list comprehensions with them, too. This one lists all of the vowels in the sentence, in the order they're seen within it:

ghci> [ x | x <- "The quick brown fox jumps over the lazy dog.", x `elem` "aeiouy" ]


In Haskell, lists are homogeneous -- they can only store one kind of value (Num, Bool, Char, etc.). If you want to store heterogeneous values, you need to use a tuple (created using parentheses):

ghci> [ True, 4, 'd']

<interactive>:18:12: error:
     Couldn't match expected type Bool with actual type Char
     In the expression: 'd'
      In the expression: [True, 4, 'd']
      In an equation for it: it = [True, 4, 'd']

ghci> (True, 4, 'd')

Haskell makes no distinction -- type-wise -- between lists of varying lengths, so long as they contain the same kind of data. So a list of lists of Nums could have sublists of multiple lengths:

ghci> [[], [0], [0,1], [0,1,2]]

This is not the case with tuples, where a pair is distinct from a triple is distinct from a 4-tuple... even if they contain the same kind of data:

ghci> [(0,1), (0,1,2)]

<interactive>:22:9: error:
     Couldn't match expected type (a, b)
                  with actual type (Integer, Integer, Integer)
     In the expression: (0, 1, 2)
      In the expression: [(0, 1), (0, 1, 2)]
      In an equation for it: it = [(0, 1), (0, 1, 2)]
     Relevant bindings include
        it :: [(a, b)] (bound at <interactive>:22:1)

Get the first element of a pair (a 2-tuple) with fst, the second element with snd:

ghci> fst ('a', 2)

ghci> snd ('a', 2)

Zip two lists element-by-element into pairs with zip. Note that the longer list (including infinite lists) is always truncated to the length of the shorter one:

ghci> zip [1..] "hello"

types and classes

You can check out the type of an object or method in ghci with the :t command:

ghci> :t (3<5) -- evaluates to True, a Bool
(3<5) :: Bool

ghci> :t "hello" -- a list of Chars ([Char]) is a String
"hello" :: [Char]

ghci> :t max -- a function which takes two Ord-erable objects of a type 'a' and returns a third
max :: Ord a => a -> a -> a

Here, a is a generic type, like T in Java. The bit of the type signature before the => is a constraint, and in this case it says that the type a must be descended from Ord (equivalent to a extends Ord in Java). If you declare a function without an explicit type signature, you can explore its inferred type signature with :t:

ghci> length' xs = sum [ 1 | _ <- xs ]

ghci> length' [1,2,3,4,5]

ghci> :t length'
length' :: Num a => [t] -> a

Above, we see that my length' method takes [t] (a list of objects of type t) and returns a, which must be an object descended from the Num-ber class. You can, and should, explicitly declare function type signatures:

ghci> let inc :: Integer -> Integer; inc x = x + 1

ghci> inc 3

ghci> :t inc
inc :: Integer -> Integer

Here, I defined the method inc, which takes an Integer and returns an Integer which has been incremented by 1.

Note that +, -, ==, /=, and so on are also functions, they're just infix functions by default. To pass them to :t (or to any other function), surround them with parentheses:

ghci> :t (+)
(+) :: Num a => a -> a -> a

ghci> :t (/=)
(/=) :: Eq a => a -> a -> Bool

built-in types

Haskell has a few predefined types, and I've already mentioned a bunch of them:

ghci> :t False -- False and True are Bool-ean types
False :: Bool

ghci> :t "hi" -- lists of Chars are synonymous with Strings
"hi" :: [Char]

ghci> x :: String; x = "hi" -- explicit type declaration

ghci> :t x
x :: String

ghci> :t 'h' -- note: Char, not [Char]
'h' :: Char

ghci> :t [1,2] -- lists are homogeneous
[1,2] :: Num a => [a]

ghci> :t ['a','b'] -- so they have type constraints
['a','b'] :: [Char]

ghci> :t [1.1,2.2]
[1.1,2.2] :: Fractional a => [a]

ghci> :t (1,'a') -- tuples have per-member type constraints
(1,'a') :: Num a => (a, Char)

ghci> :t () -- the empty tuple is a special type, the unit datatype, ()
() :: ()

ghci> :t Nothing -- Nothing and Just are of type Maybe
Nothing :: Maybe a

ghci> :t Left -- Left and Right are of type Either
Left :: a -> Either a b

ghci> :t LT -- LT, EQ, and GT are of type Ordering
LT :: Ordering

...and so on. Double, Float, Int, Integer, and other predefined types also exist in Haskell, but -- as type inference gives the variable the widest possible scope (usually Num or Fractional for numbers) -- you have to explicitly declare a variable as one of these narrower types:

ghci> x :: Double; x = 3.14

ghci> :t x
x :: Double

ghci> x :: Float; x = 3.14

ghci> :t x
x :: Float

ghci> x :: Integer; x = 42

ghci> :t x
x :: Integer

ghci> x :: Int; x = 42

ghci> :t x
x :: Int

What's the difference between Int and Integer, though? Int is bounded (and fast), but Integer is not (and slow):

ghci> factorial :: Int -> Int; factorial n = product [1..n]  

ghci> factorial 10 -- okay, this is fine

ghci> factorial 100 -- (silent) overflow!

ghci> factorial :: Integer -> Integer; factorial n = product [1..n]

ghci> factorial 100 -- woo! works great!

ghci> factorial 10000 -- also works fine! so many digits...

To me (someone with a mainly C/C++/Java background) that is pretty neat.

built-in classes

Haskell classes (also called typeclasses) are sort of like Java interfaces in that any child class derived from a particular parent class is guaranteed to implement some specific behaviour.

Some of the more common ones include:

Eq and Ord

Classes which implement Eq can be tested for equality. All predefined classes (except those related to I/O) implement Eq.

Similarly, classes which implement Ord can be ordered using <, >, and so on. All numeric types, as well as Chars and lists, extend the Ord class.


Classes which implement Show can be represented as Strings. A variable of any Show-implementing type can be converted to a String with the show method:

ghci> show True

ghci> show 1.1


Read can be thought of as the opposite of Show. The Read class parses Strings as variables of the appropriate type, where "the appropriate type" is determined by the way in which the variable is read:

ghci> read "4.8" + 2.0

ghci> read "[1,2,3]" ++ [4,5,6]

read-ing a variable and doing nothing with it will throw an error, because Haskell doesn't know what kind of type to give it:

ghci> read "4.8"
*** Exception: Prelude.read: no parse

You can get around this with an explicit type annotation:

ghci> read "4.8" :: Double


Bounded types have maximum and minimum limits. You can see what these are with minBound and maxBound:

ghci> minBound :: Int

ghci> maxBound :: Int

ghci> minBound ::Bool

ghci> maxBound :: Bool


Num is the basic numeric class in Haskell. Any class which extends Num must implement +, *, abs, signum, negation, and a few other things. Real and Fractional both derive from Num. (And Real also from Ord.)

Fractional is implemented by the predefined, non-integral numeric classes Float and Double, while Int and Integer implement the Integral class which itself implements the Real class.

A class hierarchy outlining all of this can be found in the Haskell 98 report.

As always, Learn You a Haskell has a great explanation of types and classes, and goes into more detail than I have here. I strongly recommend it.

I hope this post has jogged your memory a bit about working with list comprehensions, tuples, and types in Haskell. At this point, you should know enough to go out and complete some coding challenges! It's a great language for one-liners!

Coming up in Part 3: functions

Posted on by:

awwsmm profile

Andrew (he/him)


Got a Ph.D. looking for dark matter, but not finding any. Now I code full-time. Je parle un peu français. dogs > cats


markdown guide