简体   繁体   English

有没有办法隐藏gcd?

[英]Is there a way to hide gcd?

I am rewriting the gcd function for an assignment, but when I'm trying to hide gcd , like so:我正在重写gcd function 以进行分配,但是当我试图隐藏gcd时,如下所示:

import Prelude hiding ((gcd))

I get the error我得到错误

Parse error on input'gcd'.输入'gcd' 上的解析错误。

I'm certain I've completed the redefinition of gcd , but I can't stop the error我确定我已经完成了gcd的重新定义,但我无法阻止错误

Ambiguous occurrence, it could refer to 'Prelude.gcd'模棱两可的出现,它可以指“Prelude.gcd”

unless I manage to hide gcd .除非我设法隐藏gcd

Here is my full code (apologies can't find upload file):这是我的完整代码(抱歉找不到上传文件):

import Prelude hiding ((||)) 
import Prelude hiding ((gcd))

gcd :: Int -> Int -> Int
gcd x y
    | x == y    = x
    | x < y     = gcd x (y-x)
    | otherwise = gcd (x-y) y

And here is my error after changing to import Prelude hiding (gcd) :这是我更改为import Prelude hiding (gcd)后的错误:

错误

If you want to hide multiple things from a module, you need to import it once and specify everything to hide.如果你想从一个模块中隐藏多个东西,你需要将它导入一次并指定要隐藏的所有内容。 Importing it twice will result in each line's exclusions nullifying the other.导入它两次将导致每一行的排除使另一行无效。 So do this instead:所以改为这样做:

import Prelude hiding ((||), gcd)

(And as previously mentioned in the comments, gcd isn't an operator, so it shouldn't have parentheses around it like || does.) (正如前面在评论中提到的, gcd不是一个运算符,所以它不应该像||那样有括号。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM