# Haskell/Beginning

Note
This page is not part of the main Haskell Wikibook. It overlaps with the primary introductory material. Because learning works best when experiencing things from many angles, reading this as a supplement to reinforce basic concepts may useful.
The page here is especially thorough in explaining the basics of computing and programming, and it also clarifies some precise details about Haskell. There may be room to eventually incorporate some of the elements from this back into the main book.
Haskell is a programming language. If you can write and understand Haskell, you can create new computer programs and understand and modify programs others have written. Learning to program is a fairly complex task, but Haskell is a great way to start because it is fairly simple and predictable. Even if you end up doing most of your programming in other languages, a significant portion of your knowledge will carry over. Haskell is more than a beginning language, however; it is also one of the most advanced and powerful.

## Haskell Software

To begin using any programming language, you will need some special software to make up your development toolchain. At the minimum, you will need a compiler or an interpreter.
First, we need to reveal a little about how computers work. You may have heard of CPUs(Central Processing Units). They are pieces of hardware responsible for interpreting data known as machine language stored in the computer's memory. Machine language encodes simple instructions which, when processed by the CPU, cause the computer to do useful things such as bring you to this Web page. In other words, it is a programming language. The programs your computer executes, and the data they operate on, are stored in the same manner.
One significant consequence of this architecture is that programs can write other programs. That is how interpreters and compilers work. They translate programs written in a language such as Haskell into programs written in machine language which can then be directly executed by the computer.
The difference between a compiler and an interpreter has to do with the internal workings of the software, and you don't need to worry about it too much. Today, the difference is becoming increasingly unclear and irrelevant.
For Haskell, we use the Glasgow Haskell Compiler (GHC). It is a free/libre/open-source program, available for all major operating systems. Download

## The REPL: Using Haskell as a Calculator

GHC includes a program known as GHCi, or "GHC Interactive." This program lets you type in small Haskell programs on one line, and executes them when you hit Enter. Consult the GHC documentation for info on how to start GHCi, and do so.
You should have a prompt, which says something such as Main>, in front of you. (Don't be alarmed if it says something else, such as Prelude>). When GHCi is in this state, it is ready to accept and execute a program. Try typing in the following simple program:
GHCi should respond by displaying the number two. This is, in fact, the purpose of this program: to compute the sum of 1 and 1, and display it. Programs that compute things and display them are called expressions, and they make up the larger part of Haskell. When you execute an expression to determine the value it produces, it is referred to as evaluating that expression.
Note that, once you've evaluated an expression, you can do more than simply display it; in fact, there are a variety of ways in which the values expressions produce can be used. We will encounter these various techniques later on. For now, simply be aware that they exist.
Moving back to less theoretical matters, at this point, your interaction with GHCi should look something like this:
When showing examples, we will display interactions with GHCi this way, as well. A program will generally be included with its result, and preceded by a Main> prompt. You can duplicate the same interaction with GHCi by typing in the program on the prompt.

### More Arithmetic

As you may have guessed, Haskell supports a full complement of arithmetic operators; addition (+), subtraction (-), multiplication (*), and division (/). Numbers can be notated in the usual way, as integers or real numbers, with one catch; no number may be written without numbers before the decimal point. That is, for instance, .5 should always be written as 0.5. The operators follow the normal order of operations, and parentheses can be used in the usual way. You can also use a variety of constants and functions, such as pi, sqrt, log, sin, cos, and tan.
Go ahead and try out a few expressions; you can't break anything if you mess up. Here are some simple examples:
Note that the spaces in the examples are not necessary; the first one could have been written 3*3*3, and the fifth (7-5)*3, for instance. In general, Haskell isn't picky about spaces, except where otherwise noted. When in doubt, however, use more spaces, rather than fewer.

### Errors

Any computer user is familiar with errors. You can get them when programming, as well. Try this little experiment:
When you make a mistake in your program, GHCi will notify you, and tell you what it perceives to be the problem. Unfortunately, it doesn't always guess correctly, and it's usually rather cryptic about its diagnosis. In this example, aside from the useless comment about indentation, it's on the right track, but it still has the latter problem of being rather cryptic. So, what is a "parse error?"
A parser is the part of a compiler responsible for breaking the program down into logical pieces, and converting it to a format suitable for translation into machine language. A "parse error," then, is when the compiler can't make sense of your program; this means that there's a problem with your program.
So, what's this business: <interactive>:1:3:? The first part, <interactive>, means that it's reporting an error from the program you just typed. The first number is the number of the line that the error occurred on; it will always be 1 for interactively entered programs, so, again, it is of little use to us. The second is the column in which GHC thinks the erroneous piece of code occurs (actually, the first column is numbered zero, so it's the offending code's column number minus one). Here it points to the fourth column, just after the plus sign; this is correct, because the problem is that we omitted a right operand for the operator. (By the way, in general, do not take these numbers as Gospel; GHC sometimes gives you numbers that are slightly off.)
Now, what about when GHC guesses the problem incorrectly? Let's consider a slightly modified version of the mistake above:
We made a small change, and the error message changed completely. In addition, the message now diagnoses a problem completely different than the actual one, and refers to fairly advanced features of Haskell which you have yet to learn. An unfortunately large class of errors produces messages like this one; dealing with these messages is a more challenging aspect of learning Haskell. (Other programming systems share this problem, as well, but most are somewhat better off than Haskell.)
Of course, this was an extreme example; most of the time, the messages will not be so far off-target as this one.
Try typing in a few invalid programs and see what kinds of error messages you get back, just to get a feel for them. Again, you can't break anything, so don't worry about it. Here are some examples:

## Variables

Haskell supports a feature called variables. These are similar to the variables of algebra, but there are more restrictions on their usage.
A variable is named by one or more letters; x, jane, respond, and xyzzy are all acceptable names for variables. They can contain uppercase letters, as well, but not as the first letter; Jane, for instance, is not an acceptable variable name, although jAnE is. The typical use of uppercase letters is to delimit words in names; for instance, instead of writing multiwordvariablename, it is customary to write multiWordVariableName, which is easier on the eyes.
You can assign values to variables with a program of the form:
For instance, the program let x = 5 defines x to be five. The value can also be an arbitrary expression, such as (2 + 3) / 1.
Notice that the name of a variable is the only thing that can appear on the left side of an equals sign. These programs all work incorrectly:
Curiously, GHCi generates no errors for the latter two. That's because they're valid Haskell; they just don't do what you expect. For instance, after executing the second statement, 2 * 7 is five, and, after executing the third statement, attempting to find the value of x will result in GHCi hanging. (Press ctrl+c in UNIX, ctrl+. in Mac OS X, and ctrl+break in Windows to stop it.)
Bottom line: Haskell isn't exactly algebra; don't use it as such.
Once you've established a variable's value, you can use that variable in following expressions; each occurrence of the variable will be substituted for its value. For instance:
Multiple variables can be established in one let by delimiting the assignments with a semicolon. For instance, the previous example could be rewritten as:

## Functions

While you've made it through quite a bit of material, you may feel like you're not much closer to programming your computer. The programs we've written so far haven't accomplished much; you'd do just as well with pencil and paper or a conventional pocket calculator. By the end of this chapter, you will be able to do less trivial things, but the examples will still be quite contrived. However, at this point, you have nowhere to go but up; the variety and complexity of the programs you can write will begin to grow exponentially from here, and continue for the next several chapters, as you learn about new kinds of expressions, and new ways to combine expressions.
Haskell supports functions. To begin with, don't assume any preconceptions of this word related to mathematical functions; Haskell's functions are somewhat distinct.
A better match to Haskell functions is Haskell variables. A variable stands in for an expression. A function stands in for an expression, as well, with one twist: it takes an argument; a variable which is defined inside the function, supplied when the function is used. This concept is perhaps best understood by example:
This code defines the function f, with the argument x.
What can we do with f? We can apply it to an argument. Suppose the argument is to be 4. The code for this is:
What's going on here? Let's refer back to the definition of f:
The expression f 4 is substituted by the definition of f, with x substituted by four. Thus, it becomes 4 + 3, which is, of course, 7.
The general form of a function definition is:
And the general form of a function application is:
Notice that, in these definitions, the word "argument" is used in two different ways. The first is the kind the function has attached to its definition; simply a name standing in for a value that is supplied when it is applied. The second is the actual value that ousts the previous type of argument when the function is applied, resulting an expression which can be evaluated.
A few more examples:
Again, there is a simple technique for figuring out the value of a function expansion by hand:
* Write down the expansion of the function.
* Write it down again, with all occurrences of the argument name substituted for the argument value.
* Evaluate the resulting expression.

### Functions With Several Arguments

The function notation suggests that it might be possible to create a function with more than one argument. In fact, this is possible, and works exactly as you might expect:
One "gotcha" to watch out for when programming with multi-argument functions is giving too many arguments, or not enough arguments, to a function. These examples demonstrate the problem:
Both of these errors look fairly similar. In general, if you get an error of this form, check that you gave the right number of arguments to your functions.

### Functions in Expressions

Up until now, we have only given numbers as arguments to functions. However, you can give expressions as arguments, as well, and use function applications as expressions:
Function applications are evaluated before operators; thus, f 4 + 2 is equivalent to (f 4) + 2, not f (4 + 2).
Since function applications are expressions, they can be used in function definitions. For instance:
There are several small points to note here:
* We used the semicolon notation in the let expression to squeeze in definitions for two functions; f x = 2 * x and g x = 3 + f x.
* f is defined before g, but it doesn't have to be that way; the program would still work if the definitions were reversed. However, this does not work:
(When GHC complains that a variable or function is "not in scope," it simply means that it has not yet seen a definition of it yet. As was mentioned before, GHC requires that variables and functions be defined before they are used.)
* The definition of g, 3 + f x, is equivalent to 3 + (f x), as mandated by the rules given earlier. Thus, g 5 becomes 3 + (2 * 5) after expanding the definitions of g and f; the expression is further evaluated to produce 13.

## Conditional Tests

We promised that, as you read more of this book, you would learn to write newer, more exciting types of programs. In this chapter, we will fill in another missing piece of the puzzle.
The programs you have written so far have seemed somewhat simplistic; they haven't been able to make choices, to do different things at different times. An easy to achieve simple choice-making is to use an if expression. These expressions take the following form:
The test part can take one of the following forms:
Each form describes some sort of relation between the two expressions; for instance, < is the mathematical less-than test. Each is a plain-text corruption of some mathematical operator:
| == | The equal-to test. It is a doubled equals sign to distinguish it from the equals sign used in let expressions. |
| /= | The not-equal-to test. It is a corruption of $\neq$. |
| <, > | The less-than and greater-than tests. |
| <=, >= | The less-than-or-equal-to and greater-than-or-equal-to tests. Corruptions of $\leq$ and $\geq$. |
When an if expression is evaluated, if the test part states something true (e.g. 5 < 6), then it evaluates to the then expression; otherwise, if the test part states something false (e.g. 5 == 6), then it evaluates to the else expression. This is demonstrated in these examples:
Note that, in the last three examples, the parentheses were not necessary; however, if the last example had been written with the operands to the plus sign reversed, the parentheses would have been necessary; thus, it would be
If it were written without the parentheses, it would evaluate to:

### Examples

Here are a few examples of functions using if expressions.

#### Numerical Three-Way Tests

This function corresponds to the following mathematical function:
$f(x,p,z,n)={\begin{cases}p&{\mbox{if }}x>0;\\z&{\mbox{if }}x=0;\\n&{\mbox{if }}x<0.\end{cases}}$
The last test, instead of being if x < 0 then n, is simply else n. This is because, by the definition of the numeric relations, if x is neither less than or equal to zero, it must be greater than zero, so the test will always be true. Furthermore, every if needs an else clause, and what would you put in an else clause that can never be reached?
Actually, Haskell has a tool for these cases; undefined. This is the name of a special value that, when produced as an answer from an expression, simply flags an error. It is a good value to put in "impossible" cases of your if expressions.

#### Sign

This function corresponds to the following mathematical function:
$S(x)={\begin{cases}1&{\mbox{if }}x>0;\\0&{\mbox{if }}x=0;\\-1&{\mbox{if }}x<0.\end{cases}}$
We could have defined it like this:
However, since nif turns out to be a generalization of the sign function, it is simpler to define it as an application of nif. In fact, expanding nif into the function's body leaves you with exactly the above code!

### Boolean Expressions and Predicates
## References

* ↑ For example: is an interpreter an interpreter if what it is really doing is just compiling an expression to machine code and executing it really fast? The theoretical difference between interpretation and compilation seems to be minimal to nonexistent given w:Partial evaluation.
* ↑ There are other methods of program entry (which we are ignoring for now), which would display something else here.