Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run this haskell program in WinGHCi?

Tags:

haskell

Take this for example: http://www.haskell.org/haskellwiki/99_questions/Solutions/32

(**) Determine the greatest common divisor of two positive integer numbers. Use Euclid's algorithm.

gcd' 0 y = y
gcd' x y = gcd' (y `mod` x) x
myGCD x y | x < 0     = myGCD (-x) y
          | y < 0     = myGCD x (-y)
          | y < x     = gcd' y x
          | otherwise = gcd' x y
The Prelude includes a gcd function, so we have to choose another name for ours. The function gcd' is a straightforward implementation of Euler's algorithm, and myGCD is just a wrapper that makes sure the arguments are positive and in increasing order.

A more concise implementation is:

myGCD :: Integer -> Integer -> Integer
myGCD a b
      | b == 0     = abs a
      | otherwise  = myGCD b (a `mod` b)

How do I test this in WinGHCi? What are steps/workflow for running haskell programs?

Thanks!

like image 474
Amjad Avatar asked Dec 30 '11 15:12

Amjad


1 Answers

  1. Save the code in a .hs file somewhere, for example C:\Haskell\MyGCD.hs.

  2. Start WinGHCi and go to the directory where you saved it with :cd then load it with :load:

    Prelude> :cd C:\Haskell
    Prelude> :load MyGCD.hs
    [1 of 1] Compiling Main             ( MyGCD.hs, interpreted )
    Ok, modules loaded: Main.
    
  3. Now you can play with the function:

    *Main> myGCD 12 10
    2
    

Type :help for more info, or see Chapter 2: Using GHCi of the GHC User's Guide.

like image 56
hammar Avatar answered Oct 13 '22 07:10

hammar