Haskell:将除法结果转换为整数类型的问题

时间:2010-11-17 09:45:38

标签: haskell typeclass division collatz

我正在学习Haskell并试图理解类型系统。

我正在尝试编写一个函数,该函数返回系列'Half或Three Plus One'的长度作为输入。这是我对函数的尝试,使用递归方法(该函数仅对整数输入有效):

hotpo :: (Integral a) => a->a
hotpo n = hotpoHelper n 1

hotpoHelper:: (Integral a) => a->a->a
hotpoHelper 1 n = n
hotpoHelper num count
    | even num = hotpoHelper (truncate (num/2)) (count+1)
    | otherwise = hotpoHelper (3*num+1) (count+1)

以下是我尝试在GHC 6.12.3中加载此文件时出现的错误

test.hs:8:30:
    Could not deduce (RealFrac a) from the context (Integral a)
      arising from a use of `truncate' at test.hs:8:30-45
    Possible fix:
      add (RealFrac a) to the context of
        the type signature for `hotpoHelper'
    In the first argument of `hotpoHelper', namely
        `(truncate (num / 2))'
    In the expression: hotpoHelper (truncate (num / 2)) (count + 1)
    In the definition of `hotpoHelper':
        hotpoHelper num count
                      | even num = hotpoHelper (truncate (num / 2)) (count + 1)
                      | otherwise = hotpoHelper (3 * num + 1) (count + 1)

take (truncate (5/2)) [1,2,3]有效,所以我无法理解此错误消息。 我哪里错了?

1 个答案:

答案 0 :(得分:10)

Haskell中的/运算符用于浮点除法。如果您确实想要使用浮点除法和truncate,则首先在fromIntegral上使用num将其转换为浮点数。你得到的错误是你不能对整数使用小数除法(5/2可以工作,因为编译器会为两个数字推断浮点类型)。但是,您可以使用div功能更轻松地执行您想要的操作。这通常使用中缀,通过使用反引号包围函数名称(这适用于任何Haskell函数):

| even num = hotpoHelper (num `div` 2) (count+1)