gpt4 book ai didi

haskell - 如何在WinGHCi中运行此haskell程序?

转载 作者:行者123 更新时间:2023-12-03 14:52:39 25 4
gpt4 key购买 nike

以这个为例:
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程序的步骤/工作流程是什么?

谢谢!

最佳答案

  • 将代码保存在某个地方的.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.
  • 现在您可以使用以下功能:
    *Main> myGCD 12 10
    2

  • 键入 :help以获得更多信息,或参阅 Chapter 2: Using GHCi of the GHC User's Guide

    关于haskell - 如何在WinGHCi中运行此haskell程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8681200/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com