Haskell/Getting set up

From Wikibooks, open books for an open world
< Haskell(Redirected from Haskell/Haskell Basics)
Jump to: navigation, search

This chapter will explore how to install the programs you'll need to start coding in Haskell.

Installing Haskell[edit]

Haskell is a programming language, i.e. a language in which humans can express how computers should behave. It's like writing a cooking recipe: you write the recipe and the computer executes it.

To write Haskell programs, you need a program called a Haskell compiler. A compiler is a program that takes code written in Haskell and translates it into machine code, a more primitive language that the computer understands. Using the above analogy, the compiler is the oven that bakes your batter (code) into a cookie (executable file), and it's difficult to get the recipe from an executable once it's compiled.

To start learning Haskell, download and install the Haskell platform. It will contain the "Glasgow Haskell Compiler", or GHC, and everything else you need.

If you're just trying out Haskell, or are averse to downloading and installing the full compiler, you might like to try Hugs, the lightweight Haskell interpreter (it also happens to be portable). You might also like to play around with TryHaskell, an interpreter hosted online. Note that all instructions will be for GHC.

Note

UNIX users:

If you are a person who prefers to compile from source: This might be a bad idea with GHC, especially if it's the first time you install it. GHC is itself mostly written in Haskell, so trying to bootstrap it by hand from source is very tricky. Besides, the build takes a very long time and consumes a lot of disk space. If you are sure that you want to build GHC from the source, see Building and Porting GHC at the GHC homepage.

In short, we strongly recommend downloading the Haskell Platform instead of compiling from source.


Very first steps[edit]

After you have installed the Haskell Platform, it's now time to write your first Haskell code.

For that, you will use the program called GHCi (the 'i' stands for 'interactive'). Depending on your operating system, perform the following steps:

  • On Windows: Click Start, then Run, then type 'cmd' and hit Enter, then type ghci and hit Enter once more.
  • On MacOS: Open the application "Terminal" found in the "Applications/Utilities" folder, type the letters ghci into the window that appears and hit the Enter key.
  • On Linux: Open a terminal and run the ghci program.

You should get output that looks something like the following:

   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 6.6, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base ... linking ... done.
Prelude>

The first bit is GHCi's logo. It then informs you it's loading the base package, so you'll have access to most of the built-in functions and modules that come with GHC. Finally, the Prelude> bit is known as the prompt. This is where you enter commands, and GHCi will respond with their results.

Now you're ready to write your first Haskell code. In particular, let's try some basic arithmetic:

Prelude> 2 + 2
4
Prelude> 5 + 4 * 3
17
Prelude> 2 ^ 5
32

The operators are similar to what they are in other languages: + is addition, * is multiplication, and ^ is exponentiation (raising to the power of, or a ^ b). Note from the second example that Haskell follows standard order of operations.

Now you know how to use Haskell as a calculator. A key idea of the Haskell language is that it will always be like a calculator, except that it will become really powerful when we calculate not only with numbers, but also with other objects like characters, lists, functions, trees and even other programs (if you aren't familiar with these yet, don't worry).

GHCi is a very powerful development environment. As we progress, we'll learn how we can load source files into GHCi, and evaluate different bits of them.

The next chapter will introduce some of the basic concepts of Haskell. Let's dive into that and have a look at our first Haskell functions.

Quiz[edit]

You can attempt to answer the questions that follow to verify that you have understood the material presented here.


1. What is the program that translates Haskell code into machine code?

an interpreter
Interpreters can be used to execute code in some programming languages, and this includes Haskell (Hugs is an example of a Haskell interpreter), but they do not translate the code into machine code.
a compiler
Compilers are indeed programs that translate source code into machine code, or, in some case, byte code or simply languages of a lower level (i.e. languages that are closer to the workings of the processor and that are less abstract). GHC is the most used Haskell compiler and it compiles Haskell code to machine code.
a translator
It is true that compilation can be seen as a form of translation and that compilers can, in a way, be considered as translators, but the term “compiler” is more appropriate.
an executable
An executable file (or simply executable) is a file that can be executed as a program. The result of compilation is sometimes an executable file (sometimes accompanied by other files), but this is not always the case.

2. What is the name of the message (Prelude>) that appears before all code given to GHCi?

→ The prompt is the message that prompts the user to type code. In GHCI, it indicates the modules that are loaded. The Prelude module is always loaded, so it shows it in the prompt message when no other module is loaded.

Your score is 0 / 0