[Haskell-cafe] First steps in Haskell

Cale Gibbard cgibbard at gmail.com
Sun Dec 18 14:20:43 EST 2005

The ordinary usage pattern, which I recall is actually described in a
number of the tutorials, and on the wiki, probably in a number of
places, is to write your program text into a file with an editor, and
then load it at a terminal with either ghci fac.hs or hugs fac.hs.
(See http://www.haskell.org/hawiki/HaskellNewbie/HaskellInterpreterUsage,
as that's a conglomeration of a few of the question/answers that were
scattered about)

At that point, you can evaluate *expressions* at the ghci/hugs prompt,
to observe the operation of your program (think of it like a
debugger). This is different from entering declarations in that you
can't define things. GHCi specifically allows function/value
declarations with let (as it is somewhat emulating the inside of a
do-block), but it won't allow other kinds (data declarations, etc.).
"let x = 5 in x * x" works in either one, as it is an expression.

Note that you can type :r at the ghci or hugs prompt to reload your
file whenever you save it in your editor, so it's fairly easy to keep
things in sync.

If you want to compile your program with ghc proper, you'll need a
main action. Otherwise, well, what would the produced binary do? A
simple main is provided by printing some value, so I could add to your

main = print (fac 1000)

and then compile it, and the binary produced would print the integer
value of 1000! to the terminal.

Note that the interactive environments would have a hard time loading
ad-hoc Haskell code on stdin, as there may be, for instance, mutually
recursive bindings, and data types used in one function might be
declared later in the file. (Thus, testing that function would result
in an error message which currently isn't possible, a run-time type
error.) Further, the layout rule complicates the issue as to whether a
given line was intended as an expression, or the first part of a
declaration. I think it would be more confusing than what we currently

Hope this helps,
 - Cale

On 18/12/05, Daniel Carrera <daniel.carrera at zmsl.com> wrote:
> Hello all,
> I'm trying to write the simplest possible Haskell program, and I'm not
> getting anywhere.
> I have installed Hugs, GHC and GHCI. I want to run the following program:
> fac :: Integer -> Integer
> fac 0 = 1
> fac n | n > 0 = n * fac (n-1)
> This is what I see:
> $ hugs
> Hugs.Base> fac :: Integer -> Integer
> ERROR - Undefined variable "fac"
> Hugs.Base> fac 0 = 1
> ERROR - Syntax error in input (unexpected `=')
> $ ghci
> Prelude> fac :: Integer -> Integer
> <interactive>:1:0: Not in scope: `fac'
> Prelude> fac 0 = 1
> <interactive>:1:6: parse error on input `='
> $ # Write the program to fac.hs
> $ ghc fac.hs
> fac.hs:1:0:
>      The main function `main' is not defined in module `Main'
>      When checking the type of the main function `main'
> This is a real problem for Haskell. I expect that a lot of people try
> Haskell and give up because they can't even write the simplest function.
> It's hard not to be put off by this. I love the theory behind Haskell,
> but the practice of it seems to be a real problem.
> I hope someone will show me how to make this program work. Even better,
> I hope someone will fix the compilers and interpreters if they need
> fixing, or fix the documentation if that's what needs fixing.
> Best,
> Daniel.
> --
>       /\/`) http://oooauthors.org
>      /\/_/  http://opendocumentfellowship.org
>     /\/_/
>     \/_/    I am not over-weight, I am under-tall.
>     /
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe

More information about the Haskell-Cafe mailing list