Lets start writing simple program, which
calculates simple interest.
Let Principal = P, Rate = R% per annum
(p.a.) and Time = T years. Then,
Simple Interest = (P x R x T)/100
Step
1: Open any text editor
and type following code, save it as hello.hs. Haskell files stored with .hs
extension.
hello.hs
p = 100000 r = 1.10 t = 5 interest = p * t * r *0.01
Step 2: Go to the directory, where your hello.hs is located
and type the command ghci.
Prelude> :load hello.hs [1 of 1] Compiling Main ( hello.hs, interpreted ) Ok, modules loaded: Main. *Main> *Main> interest 5500.000000000001
‘:load’ command is used to load Haskell
file. You can use :l in short cut to load Haskell files (:l hello.hs).
Observe above code snippet, when the
file is loaded, Haskell changes the prompt from prelude to *Main. You can use
the variables defined in hello.hs in *Main prompt.
Why
the prompt changes from Prelude to Main?
When you load a file into GHCi, if the
file has module definition in it, then the Prelude is changes to the module
name. In the absence of a name for the module it is called the 'Main' module.
So you got Main prompt here. I will explain about modules later.
*Main> p 100000.0 *Main> *Main> t 1.1 *Main> *Main> r 5.0 *Main> *Main> interest 5500.000000000001 *Main>
How to reload a Haskell file?
Most of the
time, you may change the contents Haskell file, in that case you need to reload the file,
you can do that by using :reload command. You can use :r as shortcut of :reload.
*Main> :reload Ok, modules loaded: Main. *Main> :r Ok, modules loaded: Main.
No comments:
Post a Comment