Question

What are the lesser-known but useful features of the Haskell programming language. (I understand the language itself is lesser-known, but work with me. Even explanations of the simple things in Haskell, like defining the Fibonacci sequence with one line of code, will get upvoted by me.)

  • Try to limit answers to the Haskell core
  • One feature per answer
  • Give an example and short description of the feature, not just a link to documentation
  • Label the feature using bold title as the first line
Was it helpful?

Solution

My brain just exploded

If you try to compile this code:

{-# LANGUAGE ExistentialQuantification #-}
data Foo = forall a. Foo a
ignorefoo f = 1 where Foo a = f

You will get this error message:

$ ghc Foo.hs

Foo.hs:3:22:
    My brain just exploded.
    I can't handle pattern bindings for existentially-quantified constructors.
    Instead, use a case-expression, or do-notation, to unpack the constructor.
    In the binding group for
        Foo a
    In a pattern binding: Foo a = f
    In the definition of `ignorefoo':
        ignorefoo f = 1
                    where
                        Foo a = f

OTHER TIPS

User-defined control structures

Haskell has no shorthand ternary operator. The built-in if-then-else is always ternary, and is an expression (imperative languages tend to have ?:=expression, if=statement). If you want, though,

True ? x = const x
False ? _ = id

will define (?) to be the ternary operator:

(a ? b $ c)  ==  (if a then b else c)

You'd have to resort to macros in most other languages to define your own short-circuiting logical operators, but Haskell is a fully lazy language, so it just works.

-- prints "I'm alive! :)"
main = True ? putStrLn "I'm alive! :)" $ error "I'm dead :("

Hoogle

Hoogle is your friend. I admit, it's not part of the "core", so cabal install hoogle

Now you know how "if you're looking for a higher-order function, it's already there" (ephemient's comment). But how do you find that function? With hoogle!

$ hoogle "Num a => [a] -> a"
Prelude product :: Num a => [a] -> a
Prelude sum :: Num a => [a] -> a

$ hoogle "[Maybe a] -> [a]"
Data.Maybe catMaybes :: [Maybe a] -> [a]

$ hoogle "Monad m => [m a] -> m [a]"
Prelude sequence :: Monad m => [m a] -> m [a]

$ hoogle "[a] -> [b] -> (a -> b -> c) -> [c]"
Prelude zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

The hoogle-google programmer is not able to write his programs on paper by himself the same way he does with the help of the computer. But he and the machine together are a forced not* to be reckoned with.

Btw, if you liked hoogle be sure to check out hlint!

Free Theorems

Phil Wadler introduced us to the notion of a free theorem and we've been abusing them in Haskell ever since.

These wonderful artifacts of Hindley-Milner-style type systems help out with equational reasoning by using parametricity to tell you about what a function will not do.

For instance, there are two laws that every instance of Functor should satisfy:

  1. forall f g. fmap f . fmap g = fmap (f . g)
  2. fmap id = id

But, the free theorem tells us we need not bother proving the first one, but given the second it comes for 'free' just from the type signature!

fmap :: Functor f => (a -> b) -> f a -> f b

You need to be a bit careful with laziness, but this is partially covered in the original paper, and in Janis Voigtlaender's more recent paper on free theorems in the presence of seq.

Shorthand for a common list operation

The following are equivalent:

concat $ map f list
concatMap f list
list >>= f

Edit

Since more details were requested...

concat :: [[a]] -> [a]

concat takes a list of lists and concatenates them into a single list.

map :: (a -> b) -> [a] -> [b]

map maps a function over a list.

concatMap :: (a -> [b]) -> [a] -> [b]

concatMap is equivalent to (.) concat . map: map a function over a list, and concatenate the results.

class Monad m where
    (>>=) :: m a -> (a -> m b) -> m b
    return :: a -> m a

A Monad has a bind operation, which is called >>= in Haskell (or its sugared do-equivalent). List, aka [], is a Monad. If we substitute [] for m in the above:

instance Monad [] where
    (>>=) :: [a] -> (a -> [b]) -> [b]
    return :: a -> [a]

What's the natural thing for the Monad operations to do on a list? We have to satisfy the monad laws,

return a >>= f           ==  f a
ma >>= (\a -> return a)  ==  ma
(ma >>= f) >>= g         ==  ma >>= (\a -> f a >>= g)

You can verify that these laws hold if we use the implementation

instance Monad [] where
    (>>=) = concatMap
    return = (:[])

return a >>= f  ==  [a] >>= f  ==  concatMap f [a]  ==  f a
ma >>= (\a -> return a)  ==  concatMap (\a -> [a]) ma  ==  ma
(ma >>= f) >>= g  ==  concatMap g (concatMap f ma)  ==  concatMap (concatMap g . f) ma  ==  ma >>= (\a -> f a >>= g)

This is, in fact, the behavior of Monad []. As a demonstration,

double x = [x,x]
main = do
    print $ map double [1,2,3]
        -- [[1,1],[2,2],[3,3]]
    print . concat $ map double [1,2,3]
        -- [1,1,2,2,3,3]
    print $ concatMap double [1,2,3]
        -- [1,1,2,2,3,3]
    print $ [1,2,3] >>= double
        -- [1,1,2,2,3,3]

Nestable multiline comments.

{- inside a comment,
     {- inside another comment, -}
still commented! -}

Generalized algebraic data types. Here's an example interpreter where the type system lets you cover all the cases:

{-# LANGUAGE GADTs #-}
module Exp
where

data Exp a where
  Num  :: (Num a) => a -> Exp a
  Bool :: Bool -> Exp Bool
  Plus :: (Num a) => Exp a -> Exp a -> Exp a
  If   :: Exp Bool -> Exp a -> Exp a -> Exp a 
  Lt   :: (Num a, Ord a) => Exp a -> Exp a -> Exp Bool
  Lam  :: (a -> Exp b) -> Exp (a -> b)   -- higher order abstract syntax
  App  :: Exp (a -> b) -> Exp a -> Exp b
 -- deriving (Show) -- failse

eval :: Exp a -> a
eval (Num n)      = n
eval (Bool b)     = b
eval (Plus e1 e2) = eval e1 + eval e2
eval (If p t f)   = eval $ if eval p then t else f
eval (Lt e1 e2)   = eval e1 < eval e2
eval (Lam body)   = \x -> eval $ body x
eval (App f a)    = eval f $ eval a

instance Eq a => Eq (Exp a) where
  e1 == e2 = eval e1 == eval e2

instance Show (Exp a) where
  show e = "<exp>" -- very weak show instance

instance (Num a) => Num (Exp a) where
  fromInteger = Num
  (+) = Plus

Patterns in top-level bindings

five :: Int
Just five = Just 5

a, b, c :: Char
[a,b,c] = "abc"

How cool is that! Saves you that call to fromJust and head every now and then.

Optional Layout

You can use explicit braces and semicolons instead of whitespace (aka layout) to delimit blocks.

let {
      x = 40;
      y = 2
     } in
 x + y

... or equivalently...

let { x = 40; y = 2 } in x + y

... instead of ...

let x = 40
    y = 2
 in x + y

Because layout is not required, Haskell programs can be straightforwardly produced by other programs.

seq and ($!) only evaluate enough to check that something is not bottom.

The following program will only print "there".

main = print "hi " `seq` print "there"

For those unfamiliar with Haskell, Haskell is non-strict in general, meaning that an argument to a function is only evaluated if it is needed.

For example, the following prints "ignored" and terminates with success.

main = foo (error "explode!")
  where foo _ = print "ignored"

seq is known to change that behavior by evaluating to bottom if its first argument is bottom.

For example:

main = error "first" `seq` print "impossible to print"

... or equivalently, without infix ...

main = seq (error "first") (print "impossible to print")

... will blow up with an error on "first". It will never print "impossible to print".

So it might be a little surprising that even though seq is strict, it won't evaluate something the way eager languages evaluate. In particular, it won't try to force all the positive integers in the following program. Instead, it will check that [1..] isn't bottom (which can be found immediately), print "done", and exit.

main = [1..] `seq` print "done"

Operator Fixity

You can use the infix, infixl or infixr keywords to define operators associativity and precedence. Example taken from the reference:

main = print (1 +++ 2 *** 3)

infixr  6 +++
infixr  7 ***,///

(+++) :: Int -> Int -> Int
a +++ b = a + 2*b

(***) :: Int -> Int -> Int
a *** b = a - 4*b

(///) :: Int -> Int -> Int
a /// b = 2*a - 3*b
Output: -19

The number (0 to 9) after the infix allows you to define the precedence of the operator, being 9 the strongest. Infix means no associativity, whereas infixl associates left and infixr associates right.

This allows you to define complex operators to do high level operations written as simple expressions.

Note that you can also use binary functions as operators if you place them between backticks:

main = print (a `foo` b)

foo :: Int -> Int -> Int
foo a b = a + b

And as such, you can also define precedence for them:

infixr 4 `foo`

Avoiding parentheses

The (.) and ($) functions in Prelude have very convenient fixities, letting you avoid parentheses in many places. The following are equivalent:

f (g (h x))
f $ g $ h x
f . g $ h x
f . g . h $ x

flip helps too, the following are equivalent:

map (\a -> {- some long expression -}) list
flip map list $ \a ->
    {- some long expression -}

Pretty guards

Prelude defines otherwise = True, making complete guard conditions read very naturally.

fac n
  | n < 1     = 1
  | otherwise = n * fac (n-1)

C-Style Enumerations

Combining top-level pattern matching and arithmetic sequences gives us a handy way to define consecutive values:

foo : bar : baz : _ = [100 ..]    -- foo = 100, bar = 101, baz = 102

Readable function composition

Prelude defines (.) to be mathematical function composition; that is, g . f first applies f, then applies g to the result.

If you import Control.Arrow, the following are equivalent:

g . f
f >>> g

Control.Arrow provides an instance Arrow (->), and this is nice for people who don't like to read function application backwards.

let 5 = 6 in ... is valid Haskell.

Infinite Lists

Since you mentioned fibonacci, there is a very elegant way of generating fibonacci numbers from an infinite list like this:

fib@(1:tfib)    = 1 : 1 : [ a+b | (a,b) <- zip fib tfib ]

The @ operator allows you to use pattern matching on the 1:tfib structure while still referring to the whole pattern as fib.

Note that the comprehension list enters an infinite recursion, generating an infinite list. However, you can request elements from it or operate them, as long as you request a finite amount:

take 10 fib

You can also apply an operation to all elements before requesting them:

take 10 (map (\x -> x+1) fib)

This is thanks to Haskell's lazy evaluation of parameters and lists.

Flexible specification of module imports and exports

Importing and exporting is nice.

module Foo (module Bar, blah)  -- this is module Foo, export everything that Bar expored, plus blah

import qualified Some.Long.Name as Short
import Some.Long.Name (name)  -- can import multiple times, with different options

import Baz hiding (blah)  -- import everything from Baz, except something named 'blah'

If you're looking for a list or higher-order function, it's already there

There's sooo many convenience and higher-order functions in the standard library.

-- factorial can be written, using the strict HOF foldl':
fac n = Data.List.foldl' (*) 1 [1..n]
-- there's a shortcut for that:
fac n = product [1..n]
-- and it can even be written pointfree:
fac = product . enumFromTo 1

Equational Reasoning

Haskell, being purely functional allows you to read an equal sign as a real equal sign (in the absence of non-overlapping patterns).

This allows you to substitute definitions directly into code, and in terms of optimization gives a lot of leeway to the compiler about when stuff happens.

A good example of this form of reasoning can be found here:

http://www.haskell.org/pipermail/haskell-cafe/2009-March/058603.html

This also manifests itself nicely in the form of laws or RULES pragmas expected for valid members of an instance, for instance the Monad laws:

  1. returrn a >>= f == f a
  2. m >>= return == m
  3. (m >>= f) >>= g == m >>= (\x -> f x >>= g)

can often be used to simplify monadic code.

Laziness

Ubiquitous laziness means you can do things like define

fibs = 1 : 1 : zipWith (+) fibs (tail fibs)

But it also provides us with a lot of more subtle benefits in terms of syntax and reasoning.

For instance, due to strictness ML has to deal with the value restriction, and is very careful to track circular let bindings, but in Haskell, we can let every let be recursive and have no need to distinguish between val and fun. This removes a major syntactic wart from the language.

This indirectly gives rise to our lovely where clause, because we can safely move computations that may or may not be used out of the main control flow and let laziness deal with sharing the results.

We can replace (almost) all of those ML style functions that need to take () and return a value, with just a lazy computation of the value. There are reasons to avoid doing so from time to time to avoid leaking space with CAFs, but such cases are rare.

Finally, it permits unrestricted eta-reduction (\x -> f x can be replaced with f). This makes combinator oriented programming for things like parser combinators much more pleasant than working with similar constructs in a strict language.

This helps you when reasoning about programs in point-free style, or about rewriting them into point-free style and reduces argument noise.

Parallel list comprehension

(Special GHC-feature)

  fibs = 0 : 1 : [ a + b | a <- fibs | b <- tail fibs ]

Enhanced pattern matching

  • Lazy patterns
  • Irrefutable patterns

    let ~(Just x) = someExpression
    

See pattern matching

Enumerations

Any type which is an instance of Enum can be used in an arithmetic sequence, not just numbers:

alphabet :: String
alphabet = ['A' .. 'Z']

Including your own datatypes, just derive from Enum to get a default implementation:

data MyEnum = A | B | C deriving(Eq, Show, Enum)

main = do
    print $ [A ..]                 -- prints "[A,B,C]"
    print $ map fromEnum [A ..]    -- prints "[0,1,2]"

Monads

They are not that hidden, but they are simply everywhere, even where you don't think of them (Lists, Maybe-Types) ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top