小编hab*_*min的帖子

如何处理Haskell中的类型

我已经开始学习Haskell了,而且我一直在读"为了大好学习你是一个Haskell".我读完了模块章节的一半.一位朋友向我展示了代码战,我决定将我学到的一些内容用于测试.

我正在尝试创建一个函数,该函数返回给定Integral是否为4的幂的布尔值.这是代码.

module PowerOfFour where

isWhole n = fromIntegral (round n) == n

isPowerOf4 :: Integral n => n -> Bool
isPowerOf4 4 = True
isPowerOf4 x = if x < 4 then
    False
else
    if isWhole (x / 4) then
    isPowerOf4 (truncate (x / 4))
  else
    False
Run Code Online (Sandbox Code Playgroud)

这是我收到的错误消息.

/tmp/haskell11524-8-kke52v/PowerOfFour.hs:10:12:
    Could not deduce (RealFrac n) arising from a use of `isWhole'
    from the context (Integral n)
      bound by the type signature for
                 isPowerOf4 :: Integral n => n -> Bool …
Run Code Online (Sandbox Code Playgroud)

haskell types

1
推荐指数
1
解决办法
181
查看次数

标签 统计

haskell ×1

types ×1