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 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 ] [4,16,36,64,100]
The above prints the square of all values
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] ] [(1,4),(1,5),(1,6),(2,4),(2,5),(2,6),(3,4),(3,5),(3,6)]
ghci> take 10 [ x | x <- [1..], x > 10, x /= 21, odd x ] [11,13,15,17,19,23,25,27,29,31]
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] ] ["1","2","Fizz","4","Buzz","Fizz","7","8","Fizz","Buzz","11","Fizz","13","14","FizzBuzz","16","17","Fizz","19","Buzz","Fizz","22","23","Fizz","Buzz","26","Fizz","28","29","FizzBuzz","31","32","Fizz","34","Buzz","Fizz","37","38","Fizz","Buzz","41",...
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) ] [3,5,7,11,13,17,19,23,29,31]
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..] ] [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584,4181,6765]
...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" ] "euioouoeeayo"
In Haskell, lists are homogeneous -- they can only store one kind of value (
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') (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,1], [0,1,2]] [,,[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
ghci> fst ('a', 2) 'a' ghci> snd ('a', 2) 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" [(1,'h'),(2,'e'),(3,'l'),(4,'l'),(5,'o')]
You can check out the type of an object or method in
ghci with the
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
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
ghci> length' xs = sum [ 1 | _ <- xs ] ghci> length' [1,2,3,4,5] 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 4 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.
/=, 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
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.
Integer, and other predefined types also exist in Haskell, but -- as type inference gives the variable the widest possible scope (usually
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 is bounded (and fast), but
Integer is not (and slow):
To me (someone with a mainly C/C++/Java background) that is pretty neat.
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:
Classes which implement
Eq can be tested for equality. All predefined classes (except those related to I/O) implement
Similarly, classes which implement
Ord can be ordered using
>, and so on. All numeric types, as well as
Chars and lists, extend the
Classes which implement
Show can be represented as
Strings. A variable of any
Show-implementing type can be converted to a
String with the
ghci> show True "True" ghci> show 1.1 "1.1"
Read can be thought of as the opposite of
Read class parses
Strings as variables of the appropriate type, where "the appropriate type" is determined by the way in which the variable is
ghci> read "4.8" + 2.0 6.8 ghci> read "[1,2,3]" ++ [4,5,6] [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 4.8
Bounded types have maximum and minimum limits. You can see what these are with
ghci> minBound :: Int -9223372036854775808 ghci> maxBound :: Int 9223372036854775807 ghci> minBound ::Bool False ghci> maxBound :: Bool True
Num is the basic numeric class in Haskell. Any class which extends
Num must implement
signum, negation, and a few other things.
Fractional both derive from
Real also from
Fractional is implemented by the predefined, non-integral numeric classes
Integer implement the
Integral class which itself implements the
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