我的问题怎么了?

use*_*261 0 haskell

所以我刚开始学习haskell,我正在尝试使用这个if语句:

[if (((mod x 3) == 0) && ((mod x 5) == 0)) then "Fizzbuzz" else x | x <- [1..50]]
Run Code Online (Sandbox Code Playgroud)

但是当我在ghci中编译时,我收到以下错误:

No instance for (Integral [Char])
      arising from a use of `mod' at baby.hs:22:19-25
    Possible fix: add an instance declaration for (Integral [Char])
    In the first argument of `(==)', namely `(mod x 3)'
    In the first argument of `(&&)', namely `((mod x 3) == 0)'
    In the expression: (((mod x 3) == 0) && ((mod x 5) == 0))
Failed, modules loaded: none.
Run Code Online (Sandbox Code Playgroud)

好吧,所以我发现x被推断为一个字符串,因为if返回一个显式字符串,因此整个函数不起作用.那我怎么能真正解决这个问题呢?(我知道我的问题很愚蠢,但我不习惯于使用功能范例或使用类型推断进行静态输入).

Tho*_*son 7

'then'和'else'分支必须具有相同的类型. "Fizzbuzz"是一个字符串,其中x是Int.如果您打算打印结果,那么只需show x为您的else分支机构打印.

也许这可以添加到Haskell 常见误解的if/then/else部分.出于同样的原因,else分支必须存在,它也必须具有相同的类型then.