代码:
\nproduct :: (Eq p, Num p) => p -> p\nproduct 1 = 1\nproduct n = n * product (n-1)\nRun Code Online (Sandbox Code Playgroud)\n用法:
\nmain = print (product(8))\nRun Code Online (Sandbox Code Playgroud)\n错误:
\nGHCi, version 8.10.6: https://www.haskell.org/ghc/ :? for help\nLoaded GHCi configuration from /home/runner/University-Labs/.ghci\n[1 of 1] Compiling Main ( Main.hs, interpreted )\n\nMain.hs:3:17: error:\n Ambiguous occurrence \xe2\x80\x98product\xe2\x80\x99\n It could refer to\n either \xe2\x80\x98Prelude.product\xe2\x80\x99,\n imported from \xe2\x80\x98Prelude\xe2\x80\x99 at Main.hs:1:1\n (and originally defined in \xe2\x80\x98Data.Foldable\xe2\x80\x99)\n or \xe2\x80\x98Main.product\xe2\x80\x99, defined at Main.hs:2:1\n |\n3 | product n = n * product (n-1)\n | ^^^^^^^\n\nMain.hs:5:15: error:\n Ambiguous occurrence \xe2\x80\x98product\xe2\x80\x99\n It could refer to\n either \xe2\x80\x98Prelude.product\xe2\x80\x99,\n imported from \xe2\x80\x98Prelude\xe2\x80\x99 at Main.hs:1:1\n (and originally defined in \xe2\x80\x98Data.Foldable\xe2\x80\x99)\n or \xe2\x80\x98Main.product\xe2\x80\x99, defined at Main.hs:2:1\n |\n5 | main = print (product(8))\n | ^^^^^^^\nFailed, no modules loaded.\n\xee\xba\xa7 \n<interactive>:1:1: error:\n \xe2\x80\xa2 Variable not in scope: main\n \xe2\x80\xa2 Perhaps you meant \xe2\x80\x98min\xe2\x80\x99 (imported from Prelude)\n\xee\xba\xa7 ^\nRun Code Online (Sandbox Code Playgroud)\n
Sil*_*olo 10
product默认情况下会导入一个Prelude被调用的函数。所以你有两个名为 的函数product:一个是你编写的,另一个是 Haskell 内置的。
您可以随时更改函数的名称。您在那里编写的函数通常称为 Factorial ,因此您可以将名称更改为该函数。
您还可以通过其完全限定名称来引用您的函数。
main = print (Main.product 8)
Run Code Online (Sandbox Code Playgroud)
(请注意,Haskell 中未给出明确名称的模块被假定为命名为Main,因此就好像您的文件以 开头module Main where)
或者,您可以显式导入Prelude并隐藏您不需要的内容。
import Prelude hiding (product)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
178 次 |
| 最近记录: |