小编Fie*_*Foe的帖子

Haskell:Double 和 Int 之间的变量类型混淆

作为一个 uni 赋值,我应该编写一个带有类型声明的函数:

pi_approx :: Int -> Double
Run Code Online (Sandbox Code Playgroud)

这是我的第一次尝试:

pi_approx :: Int -> Double
pi_approx x = let total = sum [1 / (y^2) | y <- [1..x]]
    in sqrt (6 * total)
Run Code Online (Sandbox Code Playgroud)

这引发了以下错误:

pi_approx.hs:4:8: error:
    * Couldn't match expected type `Double' with actual type `Int'  
    * In the expression: sqrt (6 * total)
      In the expression:
        let total = sum [1 / (y ^ 2) | y <- ...] in sqrt (6 * total)
      In an equation for `pi_approx': …
Run Code Online (Sandbox Code Playgroud)

haskell types

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

标签 统计

haskell ×1

types ×1