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

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 is the Hugs prompt. This indicates that the system is ready to accept input from the user, and that it will use definitions from the module 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. The type of the putStr function, IO (), identifies it as a program to be executed rather than a value to be printed.