Haskell 不明确的类型变量'a0';由于使用“print';”而引起的;?

Haskell 不明确的类型变量'a0';由于使用“print';”而引起的;?,haskell,ambiguous-type-variable,Haskell,Ambiguous Type Variable,在学习Haskell的过程中,我试图编写一个函数,给定一个数字,它将以一个: 当我运行next 7时,我得到: <interactive>:150:1: error: * Ambiguous type variable `a0' arising from a use of `print' prevents the constraint `(Show a0)' from being solved. Probable fix: use a type annotation to s

在学习Haskell的过程中,我试图编写一个函数,给定一个数字,它将以一个:

当我运行
next 7
时,我得到:

<interactive>:150:1: error:
* Ambiguous type variable `a0' arising from a use of `print'
  prevents the constraint `(Show a0)' from being solved.
  Probable fix: use a type annotation to specify what `a0' should be.
  These potential instances exist:
    instance Show Ordering -- Defined in `GHC.Show'
    instance Show Integer -- Defined in `GHC.Show'
    instance Show a => Show (Maybe a) -- Defined in `GHC.Show'
    ...plus 22 others
    ...plus 12 instances involving out-of-scope types
    (use -fprint-potential-instances to see them all)
* In a stmt of an interactive GHCi command: print it
:150:1:错误:
*由于使用“print”而导致类型变量“a0”不明确
阻止解决约束“(显示a0)”。
可能的修复:使用类型注释指定“a0”应该是什么。
这些潜在的例子存在:
实例显示顺序--在“GHC.Show”中定义
实例Show Integer——在“GHC.Show”中定义
实例Show a=>Show(可能是a)--在“GHC.Show”中定义
…加上其他22个
…加上12个涉及范围外类型的实例
(使用-fprint潜在实例查看所有实例)
*在交互式GHCi命令的stmt中:打印它
两个问题:

  • 我是否在签名中使用了最佳类约束
  • 假设我是,我如何
    显示
    下一个7
    的结果

  • 我相信Collatz序列是一个整数序列,所以不需要将结果变为分数

    next :: (Integral a) => a -> a
    
    要从除法中获得整数,应使用
    div
    函数。请注意,除法总是精确的,因为您将只除掉偶数:

    next x
        | odd x = x * 3 + 1
        | otherwise = x `div` 2
    

    没有类型满足
    (分数a,整数a)
    。你想去掉
    小数
    ,用
    `div`
    代替
    /
    ,谢谢-这很有效,我学到了一些新东西。你想发布一个答案让我接受吗?我建议使用更具体的类型:
    Int->Int
    next x
        | odd x = x * 3 + 1
        | otherwise = x `div` 2