【问题标题】:How to run this haskell program in WinGHCi?如何在 WinGHCi 中运行这个 haskell 程序?
【发布时间】:2011-12-30 15:46:33
【问题描述】:

以此为例: 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)

如何在 WinGHCi 中进行测试?运行 haskell 程序的步骤/工作流程是什么?

谢谢!

【问题讨论】:

    标签: haskell


    【解决方案1】:
    1. 将代码保存在.hs 文件中的某个位置,例如C:\Haskell\MyGCD.hs

    2. 启动 WinGHCi 并使用:cd 转到您保存它的目录,然后使用:load 加载它:

      Prelude> :cd C:\Haskell
      Prelude> :load MyGCD.hs
      [1 of 1] Compiling Main             ( MyGCD.hs, interpreted )
      Ok, modules loaded: Main.
      
    3. 现在你可以玩这个函数了:

      *Main> myGCD 12 10
      2
      

    输入:help了解更多信息,或查看Chapter 2: Using GHCi of the GHC User's Guide

    【讨论】:

    • 啊哈..这就是您使用该功能的方式。谢谢!! :D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 2017-02-16
    • 2011-08-23
    相关资源
    最近更新 更多