Skip to content

Conventions

Throughout this book, I will (obviously) present many code examples. I will use the following conventions to distinguish between the different types of code examples I use:

  • File contents: As in most programming languages, Haskell programs are stored as plain text files. In these notes, I will use the convention that code that should be placed in a source code file starts with a header that lists the file name:

    hello_world.hs
    main :: IO ()
    main = putStrLn "Hello, world!"
    

    I may also present a file in pieces, building it up via a series of edits. in this case, the code lists the lines to be changed or added, and the header has "(Edit)" added after the file name. I'll always show enough context to make it clear where the edits should be made, like so:

    hello_world.hs (Edit)
    main :: IO ()
    main = putStrLn "Hello, world!"
    
    neverUsed :: String
    neverUsed = "This string is never used"
    

    Given the previous content of the file hello_world.hs, it should be obvious in this example that the two highlighted lines are to be added to the file.

  • Illustrations: Some code examples serve a purely illustrative purpose; they are not to be placed in a file, nor should you try them out interactively. Most of the time, bad things will happen if you try—in the sense that you get a compiler error, not in the sense that your hard drive would be formatted. I will use these examples in particular when developing ideas of how to express some things in Haskell before we are ready to put our code into a source code file or try them out. You can recognize these purely illustrative code snippets by the fact that they don't have a title.

    fst :: (a, b) -> a
    fst (x, _) = x
    
  • Interaction with the shell or ghci: There are numerous examples that show interaction with the shell or with ghci, Haskell's interactive environment. These examples will have the header "Shell" if I assume that you are starting in your shell, or "GHCi" if I assume that you have already fired up GHCi. In either case, the lines showing the prompt and the commands you are expected to enter are highlighted, whereas output produced by these commands is not. Here's an example:

    Shell
    $ stack ghci
    Prelude> putStrLn "Hello, world!"
    Hello, world!
    

    I will use $ for the shell prompt and Prelude> or, later, >>> for the GHCi prompt.

    So, if you were to try out the code in this example, you should open up a terminal window, enter stack ghci at the shell prompt to start GHCi, and then enter putStrLn "Hello, world!" at the GHCi prompt, which executes this piece of Haskell code and prints "Hello, world!" on the screen.