简体   繁体   English

对 Haskell 中数据类型的困惑

[英]Confusion about data types in Haskell

I have to write a simple pi approximation and I did and it works fine, but in the task it says to write a function with the header "pi_approx :: Int -> Double".我必须编写一个简单的 pi 近似值,我做到了,它工作正常,但是在任务中它说要编写一个带有标题“pi_approx :: Int -> Double”的函数。

My code:我的代码:

pi_approx x = sqrt (pi2_approx(x))

pi2_approx x = 
    if x/= 1
    then (6 /(x*x)) + (pi2_approx(x-1))
    else (6/(1*1))

However my function works fine without "pi_approx :: Int -> Double", but when i try to use this declaration I always get the type error:然而,我的函数在没有“pi_approx :: Int -> Double”的情况下工作正常,但是当我尝试使用这个声明时,我总是得到类型错误:

pi_approx.hs:10:14: error: pi_approx.hs:10:14: 错误:

  • Couldn't match expected type Double' with actual type Int'无法将预期类型Double' with actual type Int' 匹配
  • In the expression: (+) (6 / (x * x)) (pi2_approx (x - 1)) In the expression: if x /= 1 then (+) (6 / (x * x)) (pi2_approx (x - 1)) else (6 / (1 * 1)) In an equation for `pi2_approx': pi2_approx x = if x /= 1 then (+) (6 / (x * x)) (pi2_approx (x - 1)) else (6 / (1 * 1)) |在表达式中: (+) (6 / (x * x)) (pi2_approx (x - 1)) 在表达式中: if x /= 1 then (+) (6 / (x * x)) (pi2_approx (x - 1)) else (6 / (1 * 1)) 在 `pi2_approx' 的等式中: pi2_approx x = if x /= 1 then (+) (6 / (x * x)) (pi2_approx (x - 1) ) 否则 (6 / (1 * 1)) | 10 | 10 | then (+) (6 /(x*x)) (pi2_approx(x-1)) |然后 (+) (6 /(x*x)) (pi2_approx(x-1)) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I tried to use fromIntegral in various ways and if I run the function without declaration I checked the type of the solution, which is: "(Floating a, Eq a) => a"我尝试以各种方式使用 fromIntegral,如果我在没有声明的情况下运行该函数,我检查了解决方案的类型,即:“(Floating a, Eq a) => a”

As I understand Double should be an instance of Floating so I don´t understand why it wont compile.据我了解 Double 应该是 Floating 的一个实例,所以我不明白为什么它不会编译。

I am very new at haskell and it seems I don´t understand the core concept of data types and constraints.我是haskell 的新手,似乎我不了解数据类型和约束的核心概念。 I can´t seem to find any simple and good documentation/explanation on the topic though.不过,我似乎找不到任何关于该主题的简单而好的文档/解释。 Maybe someone here can help me understand based on this example :)也许这里有人可以根据这个例子帮助我理解:)

because x is an Int , hence x * x is also an Int , and you can not use an Int for (/) :: Floating a => a -> a -> a .因为xInt ,因此x * x也是Int ,并且不能将Int用于(/) :: Floating a => a -> a -> a

You need to convert this to a Double , for example with fromIntegral :: (Integral a, Num b) => a -> b :您需要将其转换为Double ,例如使用fromIntegral :: (Integral a, Num b) => a -> b

pi2_approx :: Int -> Double
pi2_approx 1 = 6
pi2_approx x = 6 / fromIntegral (x*x) + pi2_approx (x-1)

For a large number of iterations, it gives a result close to π 2 :对于大量迭代,它给出接近π 2的结果:

Prelude> sqrt (pi2_approx 10000)
3.1414971639472147

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM