作为一个 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)