1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
<body bgcolor="#ffffff"> <i>The Hugs 98 User Manual</i><br> <a href="index.html">top</a> | <a href="summary.html">back</a> | <a href="started.html">next</a> <br><hr>
<a name="basics"></a><a name="sect3"></a>
<h2>3<tt> </tt>Hugs for beginners</h2>
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 <tt>hugs</tt>, 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 <tt>.hs</tt> or <tt>.lhs</tt> file.):
<tt><br>
__ __ __ __ ____ ___ _________________________________________<br>
|| || || || || || ||__ Hugs 98: Based on the Haskell 98 standard<br>
||___|| ||__|| ||__|| __|| Copyright (c) 1994-1999<br>
||---|| ___|| World Wide Web: http://haskell.org/hugs<br>
|| || Report bugs to: hugs-bugs@haskell.org<br>
|| || Version: September 1999 _________________________________________<br>
<br>
Haskell 98 mode: Restart with command line option -98 to enable extensions<br>
<br>
Reading file "/Hugs/lib/Prelude.hs":<br>
<br>
Hugs session for:<br>
/Hugs/lib/Prelude.hs<br>
Type :? for help<br>
Prelude><br>
</tt>The file <tt>/Hugs/lib/Prelude.hs</tt> 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.<a name="secexpr"></a><p>
<a name="sect3.1"></a>
<h3>3.1<tt> </tt>Expressions</h3>
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.
<tt><br>
Prelude> (2+3)*8<br>
40<br>
<br>
Prelude> sum [1..10]<br>
55<br>
Prelude><br>
</tt>The <tt>Prelude></tt> 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 <tt>Prelude</tt> 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 <tt>+</tt>, <tt>*</tt>, and <tt>sum</tt>.
In response to the first prompt, the user entered the
expression <tt>(2+3)*8</tt>, which was evaluated to produce the
result <tt>40</tt>. In response to the second prompt, the user typed the
expression <tt>sum [1..10]</tt>. The notation <tt>[1..10]</tt> represents
the list of integers between 1 and 10 inclusive, and <tt>sum</tt> is a
prelude function that calculates the sum of a list of numbers. So the
result obtained by Hugs is:
<tt><br>
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55.<br>
</tt>In fact, we could have typed this sum directly into Hugs:
<tt><br>
Prelude> 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10<br>
55<br>
Prelude> <br>
</tt>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:
<tt><br>
Prelude> (not True) || False<br>
False<br>
Prelude> reverse "Hugs is cool"<br>
"looc si sguH"<br>
Prelude> filter even [1..10] <br>
[2, 4, 6, 8, 10]<br>
Prelude> take 10 fibs where fibs = 0:1:zipWith (+) fibs (tail fibs)<br>
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]<br>
Prelude><br>
</tt>You cannot create new definitions at the command prompt---these must
be placed in files and loaded, as described later. The definition
of <tt>fib</tt> 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.<p>
Hugs even allows whole programs to be used as values
in calculations.
For example, <tt>putStr "hello, "</tt> is a simple program that outputs the
string <tt>"hello, "</tt>. Combining this with a similar program to print
the string <tt>"world"</tt>, gives:
<tt><br>
Prelude> putStr "hello, " >> putStr "world"<br>
hello, world<br>
Prelude><br>
</tt>Just as there are standard operations for dealing with numbers, so there
are standard operations for dealing with programs. For example,
the <tt>>></tt> 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 <tt>putStr "world"</tt> has type <tt>IO ()</tt>, which identifies
it as a program to be executed rather than a value to be printed.<a name="seccommands"></a><p>
<a name="sect3.2"></a>
<h3>3.2<tt> </tt>Commands</h3>
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:
<UL><LI><tt>:q</tt> exits the interpreter.
On most systems, you can also terminate Hugs by typing the
end-of-file character.
<LI><tt>:?</tt> prints a list of
all the commands, which can be useful if you forget the name of
the command that you want to use.
</UL>
Like most other commands in Hugs, these commands both start with
a colon, <tt>:</tt>.<p>
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
<tt>{Interrupted!}</tt> and returns to the prompt so that further
commands can be entered.<a name="secscripts"></a><p>
<a name="sect3.3"></a>
<h3>3.3<tt> </tt>Programs</h3>
Functions like <tt>sum</tt>, <tt>>></tt> and <tt>take</tt>, 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:
<tt><br>
module Fact where<br>
fact :: Integer -> Integer<br>
fact n = product [1..n]<br>
</tt>into a file called <tt>Fact.hs</tt>. (By convention, Hugs modules are stored
in files ending with the characters <tt>.hs</tt>. The file name should
match the name of the module it contains.)
The <tt>product</tt> 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 <tt>sum</tt> to calculate the corresponding sum.
So the line above defines a function <tt>fact</tt> that takes an
argument <tt>n</tt> and calculates its factorial. In standard mathematical
notation, <tt>fact n = n!</tt>, which is usually defined by an equation:
<tt><br>
n! = 1 * 2 * ... * (n-1) * n<br>
</tt>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 <tt>n</tt> is the product of the numbers from 1 to <tt>n</tt>.<p>
Before we can use this definition in a Hugs session, we have to
load <tt>Fact.hs</tt> into the interpreter. One of the simplest ways to do
this uses the <tt>:load</tt> command:
<tt><br>
Prelude> :load fact.hs <br>
Reading file "fact.hs":<br>
<br>
Hugs session for:<br>
/Hugs/lib/Prelude.hs<br>
Fact.hs<br>
Fact> <br>
</tt>Notice the list of filenames displayed after <tt>Hugs session for:</tt>; 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 <tt>Fact</tt> and evaluation will take place within this
new module. We can start to use
the <tt>fact</tt> function that we have defined:
<tt><br>
Fact> fact 6<br>
720<br>
Fact> fact 6 + fact 7<br>
5760<br>
Fact> fact 7 `div` fact 6<br>
7<br>
Fact><br>
</tt>As another example, the standard formula for the number of
different ways of choosing <tt>r</tt> objects from a collection
of <tt>n</tt> objects is n!/(r!(n-r)!). A simple
and direct (but otherwise not particularly good) definition for
this function in Hugs is as follows:
<tt><br>
comb n r = fact n `div` (fact r * fact (n-r))<br>
</tt>One way to use this function is to include its definition as part
of an expression entered in directly to Hugs:
<tt><br>
Fact> comb 5 2 where comb n r = fact n `div` (fact r * fact (n-r))<br>
10<br>
Fact> <br>
</tt>The definition of <tt>comb</tt> here is local to this expression. If we want
to use <tt>comb</tt> several times, then it would be sensible
to add its definition to the file <tt>Fact.hs</tt>. Once this has been
done, and the <tt>Fact.hs</tt> file has been reloaded, then we can use
the <tt>comb</tt> function like any other built-in operator:
<tt><br>
Fact> :reload<br>
Reading file "fact.hs":<br>
<br>
Hugs session for:<br>
/Hugs/lib/Prelude.hs<br>
Fact.hs<br>
Fact> comb 5 2<br>
10<br>
Fact><br>
<p>
<hr><i>The Hugs 98 User Manual</i><br><a href="index.html">top</a> | <a href="summary.html">back</a> | <a href="started.html">next</a> <br><font size=2>May 22, 1999</font>
</tt>
|