我正在尝试加载以下程序
gcd a b = if b == 0
then a
else gcd b (rem a b)
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误
Prelude> :l euclidean.hs
[1 of 1] Compiling Main ( euclidean.hs, interpreted )
euclidean.hs:3:8:
Ambiguous occurrence `gcd'
It could refer to either `Main.gcd', defined at euclidean.hs:1:0
or `Prelude.gcd', imported from Prelude
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)
我从改变函数名gcd来main和我
Couldn't match expected type `IO t'
against inferred type `a -> a -> a'
In the expression: main
When checking the type of the function `main'
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)
我不明白这一点.我在这里使用研讨会的一步.
Tik*_*vis 12
第一个错误应该是不言而喻的 - 一个gcd已经存在的函数.
第二个也很简单.在Haskell中,main是程序的切入点.由于程序需要某种方式来做IO ,通常main必须在表单中有一个类型.这意味着你应该把你的功能称为别的东西.(Haskell中的函数类似于Java中的方法.)IO aIO ()gcdmainmain
通常的做法是调用它gcd',发音为"gcd prime".在这种情况下,命名您的函数gcd'表示它只是一个不同的实现gcd.
| 归档时间: |
|
| 查看次数: |
215 次 |
| 最近记录: |