以此为例: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)
Run Code Online (Sandbox Code Playgroud)
如何在WinGHCi中测试?运行haskell程序的步骤/工作流程是什么?
谢谢!
ham*_*mar 11
.hs例如,将代码保存在某个文件中C:\Haskell\MyGCD.hs.
启动WinGHCi并转到保存它的目录,:cd然后加载它:load:
Prelude> :cd C:\Haskell
Prelude> :load MyGCD.hs
[1 of 1] Compiling Main ( MyGCD.hs, interpreted )
Ok, modules loaded: Main.
Run Code Online (Sandbox Code Playgroud)现在您可以使用该功能:
*Main> myGCD 12 10
2
Run Code Online (Sandbox Code Playgroud)键入:help更多信息,或参见第2章:使用GHC用户指南GHCI.