The Hugs 98 User Manual
top | back | next

3  Hugs for beginners

This section covers the basics that you need to understand to start using Hugs. Most of the points discussed here will be familiar to anyone with experience of previous versions of Hugs or Gofer. To begin with, we need to start the interpreter; the usual way to do this is by using the command hugs, which produces a startup banner something like the following (On Windows 95/NT, the installation procedure normally adds Hugs to the start menu. You can also start the interpreter by double clicking on a .hs or .lhs file.):
 __   __ __  __  ____   ___       _________________________________________
 ||   || ||  || ||  || ||__       Hugs 98: Based on the Haskell 98 standard
 ||___|| ||__|| ||__||  __||      Copyright (c) 1994-1999
 ||---||         ___||            World Wide Web: http://haskell.org/hugs
 ||   ||                          Report bugs to: hugs-bugs@haskell.org
 ||   || Version: September 1999  _________________________________________
 
 Haskell 98 mode: Restart with command line option -98 to enable extensions
 
 Reading file "/Hugs/lib/Prelude.hs":
 
 Hugs session for:
 /Hugs/lib/Prelude.hs
 Type :? for help
 Prelude>
The file /Hugs/lib/Prelude.hs mentioned here contains standard definitions that are loaded into Hugs each time that the interpreter is started; the filename will vary from one installation to the next (If Hugs does not load correctly, and complains that it cannot find the prelude, then Hugs has not been installed correctly and you should look at the installation instructions.). You may notice a pause while the interpreter is initialized and the prelude definitions are loaded into the system.

3.1  Expressions

In essence, using Hugs is just like using a calculator; the interpreter simply evaluates each expression that is entered, printing the results as it goes.
 Prelude> (2+3)*8
 40

 Prelude> sum [1..10]
 55
 Prelude>
The Prelude> characters at the begining of the first, third and fifth lines here form the Hugs prompt. This indicates that the system is ready to accept input from the user, and that it will use definitions from the Prelude module to evaluate each expression that is entered; The Hugs prelude is a special module that contains definitions for the built-in operations of Haskell, such as +, *, and sum. In response to the first prompt, the user entered the expression (2+3)*8, which was evaluated to produce the result 40. In response to the second prompt, the user typed the expression sum [1..10]. The notation [1..10] represents the list of integers between 1 and 10 inclusive, and sum is a prelude function that calculates the sum of a list of numbers. So the result obtained by Hugs is:
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10  =  55.
In fact, we could have typed this sum directly into Hugs:
 Prelude> 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10
 55
 Prelude> 
Unlike many calculators, however, Hugs is not limited to working with numbers; expressions can involve many different types of value, including numbers, booleans, characters, strings, lists, functions, and user-defined datatypes. Some of these are illustrated in the following example:
 Prelude> (not True) || False
 False
 Prelude> reverse "Hugs is cool"
 "looc si sguH"
 Prelude> filter even [1..10]     
 [2, 4, 6, 8, 10]
 Prelude> take 10 fibs where fibs = 0:1:zipWith (+) fibs (tail fibs)
 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
 Prelude>
You cannot create new definitions at the command prompt---these must be placed in files and loaded, as described later. The definition of fib in the last example above is local to that expression and will not be remembered for later use. Also, the expressions entered must fit on a single line.

Hugs even allows whole programs to be used as values in calculations. For example, putStr "hello, " is a simple program that outputs the string "hello, ". Combining this with a similar program to print the string "world", gives:
 Prelude> putStr "hello, " >> putStr "world"
 hello, world
 Prelude>
Just as there are standard operations for dealing with numbers, so there are standard operations for dealing with programs. For example, the >> operator used here constructs a new program from the programs supplied as its operands, running one after the other. Normally, Hugs just prints the value of each expression entered. But, as this example shows, if the expression evaluates to a program, then Hugs will run it instead. Hugs distinguishes programs from other expressions by looking at the type of the expression entered. For example, the expression putStr "world" has type IO (), which identifies it as a program to be executed rather than a value to be printed.

3.2  Commands

Each line that you enter in response to the Hugs prompt is treated as a command to the interpreter. For example, when you enter an expression into Hugs, it is treated as a command to evaluate that expression, and to display the result. There are two commands that are particularly worth remembering: Like most other commands in Hugs, these commands both start with a colon, :.

Note that the interrupt key (control-C or control-Break on most systems) can be used to abandon the process of compiling files or evaluating expressions. When the interrupt is detected, Hugs prints {Interrupted!} and returns to the prompt so that further commands can be entered.

3.3  Programs

Functions like sum, >> and take, used in the examples above, are all defined in the Hugs prelude; you can actually do quite a lot using just the types and operations provided by the prelude. But, in general, you will also want to define new types and operations, storing them in modules that can be loaded and used by Hugs. A module is simply a collection of definitions stored in a file. For example, suppose we enter the following module:
 module Fact where
 fact  :: Integer -> Integer
 fact n = product [1..n]
into a file called Fact.hs. (By convention, Hugs modules are stored in files ending with the characters .hs. The file name should match the name of the module it contains.) The product function used here is also defined in the prelude, and can be used to calculate the product of a list of numbers, just as you might use sum to calculate the corresponding sum. So the line above defines a function fact that takes an argument n and calculates its factorial. In standard mathematical notation, fact n = n!, which is usually defined by an equation:
 n! = 1 * 2 * ... * (n-1) * n
Once you become familiar with the notation, you will see that the Hugs definition is really very similar to this informal, mathematical version: the factorial of a number n is the product of the numbers from 1 to n.

Before we can use this definition in a Hugs session, we have to load Fact.hs into the interpreter. One of the simplest ways to do this uses the :load command:
 Prelude> :load fact.hs 
 Reading file "fact.hs":

 Hugs session for:
 /Hugs/lib/Prelude.hs
 Fact.hs
 Fact> 
Notice the list of filenames displayed after Hugs session for:; this tells you which module files are currently being used by Hugs, the first of which is always the standard prelude. The prompt is now Fact and evaluation will take place within this new module. We can start to use the fact function that we have defined:
 Fact> fact 6
 720
 Fact> fact 6 + fact 7
 5760
 Fact> fact 7 `div` fact 6
 7
 Fact>
As another example, the standard formula for the number of different ways of choosing r objects from a collection of n objects is n!/(r!(n-r)!). A simple and direct (but otherwise not particularly good) definition for this function in Hugs is as follows:
 comb n r = fact n `div` (fact r * fact (n-r))
One way to use this function is to include its definition as part of an expression entered in directly to Hugs:
 Fact> comb 5 2 where comb n r = fact n `div` (fact r * fact (n-r))
 10
 Fact> 
The definition of comb here is local to this expression. If we want to use comb several times, then it would be sensible to add its definition to the file Fact.hs. Once this has been done, and the Fact.hs file has been reloaded, then we can use the comb function like any other built-in operator:
 Fact> :reload
 Reading file "fact.hs":

 Hugs session for:
 /Hugs/lib/Prelude.hs
 Fact.hs
 Fact> comb 5 2
 10
 Fact>


The Hugs 98 User Manual
top | back | next
May 22, 1999