Learn You a Haskell for Great Good! - Miran Lipovaca [2]
If you’ve defined some functions in a script—for example, myfunctions.hs—you can load these functions into GHCi by typing :l myfunctions. (Make sure that myfunctions.hs is in the same folder from which you started GHCi.)
If you change the .hs script, run :l myfunctions to load the file again or run :r, which reloads the current script. My usual workflow is to define some functions in an .hs file, load it into GHCi, mess around with it, change the file, and repeat. This is what we’ll be doing in this book.
Acknowledgments
Thanks to everyone who sent in corrections, suggestions, and words of encouragement. Also thanks to Keith, Sam, and Marilyn for making me look like a real writer.
Chapter 1. Starting Out
If you’re the horrible sort of person who doesn’t read introductions, you might want to go back and read the last section anyway—it explains how to use this book, as well as how to load functions with GHC.
First, let’s start GHC’s interactive mode and call some functions, so we can get a very basic feel for Haskell. Open a terminal and type ghci. You will be greeted with something like this:
GHCi, version 6.12.3: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.
Note
GHCi’s default prompt is Prelude>, but we’ll be using ghci> as our prompt for the examples in this book. To make your prompt match the book’s, enter :set prompt "ghci> " into GHCi. If you don’t want to do this every time you run GHCi, create a file called .ghci in your home folder and set its contents to :set prompt "ghci> ".
Congratulations, you’re in GHCi! Now let’s try some simple arithmetic:
ghci> 2 + 15
17
ghci> 49 * 100
4900
ghci> 1892 - 1472
420
ghci> 5 / 2
2.5
If we use several operators in one expression, Haskell will execute them in an order that takes into account the precedence of the operators. For instance, * has higher precedence than -, so 50 * 100 - 4999 is treated as (50 * 100) - 4999.
We can also use parentheses to explicitly specify the order of operations, like this:
ghci> (50 * 100) - 4999
1
ghci> 50 * 100 - 4999
1
ghci> 50 * (100 - 4999)
-244950
Pretty cool, huh? (Yeah, I know it’s not, yet, but bear with me.)
One pitfall to watch out for is negative number constants. It’s always best to surround these with parentheses wherever they occur in an arithmetic expression. For example, entering 5 * -3 will make GHCi yell at you, but entering 5 * (-3) will work just fine.
Boolean algebra is also straightforward in Haskell. Like many other programming languages, Haskell has the Boolean values True and False, and uses the && operator for conjunction (Boolean and), the || operator for disjunction (Boolean or), and the not operator to negate a True or False value:
ghci> True && False
False
ghci> True && True
True
ghci> False || True
True
ghci> not False
True
ghci> not (True && True)
False
We can test two values for equality or inequality with the == and /= operators, like this:
ghci> 5 == 5
True
ghci> 1 == 0
False
ghci> 5 /= 5
False
ghci> 5 /= 4
True
ghci> "hello" == "hello"
True
Watch out when mixing and matching values, however! If we enter something like 5 + "llama", we get the following error message:
No instance for (Num [Char])
arising from a use of `+' at Possible fix: add an instance declaration for (Num [Char]) In the expression: 5 + "llama" In the definition of `it': it = 5 + "llama" What GHCi is telling us here is that "llama" is not a number, so it does not know how to add it to 5. The + operator expects both of its inputs to be numbers. On the other hand, the == operator works