2011-12-30 14 views
5

Cầm lấy cái này ví dụ: http://www.haskell.org/haskellwiki/99_questions/Solutions/32Làm thế nào để chạy chương trình haskell này trong WinGHCi?

(**) 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) 

Làm thế nào để kiểm tra điều này trong WinGHCi? Các bước/quy trình làm việc để chạy các chương trình haskell là gì?

Cảm ơn!

Trả lời

11
  1. Save the mã trong một tập tin .hs nơi nào đó, ví dụ C:\Haskell\MyGCD.hs.

  2. Bắt đầu WinGHCi và đi đến thư mục mà bạn đã lưu nó với :cd sau đó tải nó với :load:

    Prelude> :cd C:\Haskell 
    Prelude> :load MyGCD.hs 
    [1 of 1] Compiling Main    (MyGCD.hs, interpreted) 
    Ok, modules loaded: Main. 
    
  3. Bây giờ bạn có thể chơi với các chức năng:

    *Main> myGCD 12 10 
    2 
    

Nhập :help để biết thêm thông tin hoặc xem Chapter 2: Using GHCi of the GHC User's Guide.

+0

Aha .. đó là cách bạn chơi với chức năng. Cảm ơn bạn!! : D – Amjad

Các vấn đề liên quan