<A target="_top" HREF="manual_contents.html"><img align=center src="contents.gif" ALT="Contents"></A> Up Previous Next

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! `div` (r! * (n-r)!). One 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>