为什么不能在Haskell中添加一个Int和一个浮点数 [英] why cant an Int and a floating point number be added in haskell

查看:88
本文介绍了为什么不能在Haskell中添加一个Int和一个浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (长度[1,2,3,4])+ 3.2 

尽管如此: -

  2 + 3.3 

我知道在第一种情况下,结果是一个Int +浮点数,但在第二种情况下也不一样,或者Haskell在第二种情况下自动推断出类型为: - Num + Num,而在第一种情况下不会这样做?

解决方案

Haskell 从不为您进行隐式类型转换。 + 只能处理两个相同类型的数字,并且也会给出该类型作为结果。正如您在(长度[1,2,3,4])+ 3.2 中看到的那样, + 的任何其他用法都是错误的c $ c> example。



但是,数字文字在Haskell中被重载。 2 可以是任何数字类型, 3.3 可以是任何小数类型。所以当Haskell看到表达式 2 + 3.3 时,它可以尝试找到一个既是数字也是小数的类型,并将这两个数字视为该类型,

更准确地说, + 的类型为 Num a => ; a - > a - >一个 2 本身是类型 Num a =>一个 3.3 本身是类型小数a =>一个。将这3种类型放在一起,在 2 + 3.3 这两个表达式中可以给出类型 Fractional a =>一个,因为所有小数类型也是 Num 类型,这也满足类型 + 。 (如果你在GHCi中输入这个表达式, a 被填充为 Double ,因为GHC必须将类型默认为 为了评估它)



在表达式中(长度[1,2,3,4]) + 3.2 3.2 仍然被重载(并且隔离的类型为 Fractional a => a )。但长度[1,2,3,4] 的类型为 Int 。由于一方是固定的具体类型,满足 + 类型的唯一方法是填写 a Int 的其他类型上,但违反了小数约束;没有办法让 3.2 成为 Int 。所以这个表达式不是很好的类型。然而,任何 Integral 类型(其中 Int 是一个)可以通过应用 fromIntegral 来转换为任何 Num code>(这实际上是如何将像 2 这样的整数文字视为任何数字类型)。因此,(fromIntegral $ length [1,2,3,4])+ 3.2 将可用。


why wont this work :-

(length [1,2,3,4]) + 3.2

while this works:-

2+3.3

I understand that in the first case the result is an Int+Float but is that not the same in the second case too, or does Haskell automatically infer the type in the second case to be :- Num+Num whereas it does not do that in the first case?

解决方案

Haskell never does implicit type conversion for you. + only ever works on two numbers of the same type, and gives you that type as the result as well. Any other usage of + is an error, as you saw with your (length [1,2,3,4]) + 3.2 example.

However, numeric literals are overloaded in Haskell. 2 could be any numeric type, and 3.3 could be any fractional type. So when Haskell sees the expression 2 + 3.3 it can try to find a type which is both "numeric" and "fractional", and treat both numbers as that type so that the addition will work.

Speaking more precisely, + has the type Num a => a -> a -> a. 2 on its own is of type Num a => a and 3.3 on its own is of type Fractional a => a. Putting those 3 types together, the in the expression 2 + 3.3 both numbers can be given the type Fractional a => a, because all Fractional types are also Num types, and this then also satisfies the type of +. (If you type this expression into GHCi the a gets filled in as Double, because GHC has to default the type to something in order to evaluate it)

In the expression (length [1,2,3,4]) + 3.2, the 3.2 is still overloaded (and in isolation would have type Fractional a => a). But length [1,2,3,4] has type Int. Since one side is a fixed concrete type, the only way to satisfy the type for + would be to fill in the a on the other type with Int, but that violates the Fractional constraint; there's no way for 3.2 to be an Int. So this expression is not well-typed.

However, any Integral type (of which Int is one) can be converted to any Num type by applying fromIntegral (this is actually how the integer literals like 2 can be treated as any numeric type). So (fromIntegral $ length [1,2,3,4]) + 3.2 will work.

这篇关于为什么不能在Haskell中添加一个Int和一个浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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