是什么导致Haskell这样的类型错误? [英] What causes a type error like this in Haskell?

查看:44
本文介绍了是什么导致Haskell这样的类型错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Haskell一起使用值表评估简单极限.我定义了以下函数:

  f ::(积分a)=>a->一种f x = div 1 $减去6 x 

在GHCI中,我 let leftSide = [5.90,5.91..5.99] let rightSide = [6.10,6.09..6.01] ,然后:

  GHCI>左图 

阻止此错误的原因:

 < interactive>:50:5没有因使用'f'而引起的(Integral Double)实例可能的解决方法:为(Integral Double)添加一个实例声明在"map"的第一个参数中,即"f"在表达式中:map f leftSide在"it"的方程式中:it = map f leftSide 

将f的类型声明更改为(Integral Double a)=>a->一个使编译器抱怨"Integral"如何应用于太多类型参数.这是怎么回事?

解决方案

我认为您只是尝试了错误的-您确实希望(/)而不是div ...

您的问题是 div 需要一个 Integral 类型(例如,一个 Integer ):

  Prelude>:t divdiv ::积分a =>a->a->一种 

,然后通过 map leftSide rightSide

现在GHCi将其默认设置为 Double -但 Double 不是 Integral 的实例-这正是Haskell抱怨的./p>


您尝试的操作不起作用,我想您想编写(整数a,双精度a)=>... ,但最后(如果可行,因为 Double 是类型而不是类型类,所以不会),它与说 f相同::双->仍然是Double -会再次出现错误(因为 Double 没有 div )

简而言之:使用(/)代替 div ,它应该可以工作:

  f ::小数r =>r->[Rf x =(1//$)减去6 x 

这是您的第一个示例:

  Prelude>let leftSide = [5.90,5.91..5.99]<前奏曲>左图[-10.000000000000036,-11.111111111111128,-12.49999999999999,-14.285714285714228,-16.66666666666653,-19.999999999999716,-24.999999999999424,-33.33333333333207,-49.999999999996625,-99.99999999998437] 


btw:如果您使用无点模式,则可以使此更短:

  f =(1//).(6-) 

或者更简洁/可读性更强

  f x = 1/(6-x) 

I'm playing with Haskell to evaluate simple limits with tables of values. I have the following function defined:

f :: (Integral a) => a -> a
f x = div 1 $ subtract 6 x

and in GHCI, I let leftSide = [5.90, 5.91..5.99] and let rightSide = [6.10,6.09..6.01], then:

GHCI> map f leftSide

Which barfs up this error:

<interactive>:50:5
  No instance for (Integral Double) arising from a use of `f'
  Possible fix: add an instance declaration for (Integral Double)
  In the first argument of `map', namely `f'
  In the expression: map f leftSide
  In an equation for `it': it = map f leftSide

Changing my type declaration for f to (Integral Double a) => a -> a makes the compiler complain about how `Integral' is applied to too many type arguments. What's going on here?

解决方案

I think you just tried the wrong division - you really want (/) not div...

your problem is that div which demands an Integral type (for example an Integer):

Prelude> :t div
div :: Integral a => a -> a -> a

but then you use it with a Fractional (6.10, ...) by maping it over your leftSide and rightSide

now GHCi defaults this to Double - but Double is not an instance of Integral - this is exactly what Haskell is complaining about.


the thing you tried does not work and I guess you wanted to write (Integral a, Double a) => ... but in the end (if it would work - it does not since Double is type and not a type-class) it would be the same as saying f :: Double -> Double anyway - which would get you the error again (as there is no div for Double)

To make it short: use (/) instead of div and it should work:

f :: Fractional r => r -> r
f x = (1 /) $ subtract 6 x

here is your first example:

Prelude> let leftSide = [5.90, 5.91..5.99]
Prelude> map f leftSide
[-10.000000000000036,-11.111111111111128
,-12.49999999999999,-14.285714285714228
,-16.66666666666653,-19.999999999999716
,-24.999999999999424,-33.33333333333207
,-49.999999999996625,-99.99999999998437]


btw: you can make this shorter if you use point-free:

f = (1 /) . (6 -)

or a bit cleaner / more readable if you write it out

f x = 1 / (6 - x)

这篇关于是什么导致Haskell这样的类型错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆