本文介绍了Haskell中的数字解释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要清楚解释 Num , Real ,积分,整数, Int ,比率, Rational , Double , Float 。

解决方案

这个答案主要假设你知道类型和类型的区别。如果这种差异对您很朦胧,那么请在阅读之前。



Num



Num 是包括所有数字类型的类型类型。

 :info数字
类数字其中
(+): :a - > a - > a
( - ):: a - > a - > a
(*):: a - > a - > a
negate :: a - >
abs :: a - >
signum :: a - > a
fromInteger :: Integer - > a
- 定义于'GHC.Num'
实例Num Word - 定义于'GHC.Num'
实例Num整数 - 定义于'GHC.Num'
实例Num Int - 定义于'GHC.Num'
实例Num Float - 定义于'GHC.Float'
实例Num Double - 定义于'GHC.Float'

真正的

那些可以表示为实际值的类型( Rational 类型)。

 :info Real 
class(Num a,Ord a)=> Real a where
toRational :: a - >理性
- 定义于'GHC.Real'
实例真实单词 - 定义于'GHC.Real'
实例整数 - 定义于'GHC.Real'
实例 - 在'GHC.Real'中定义
实例Real Float - 在'GHC.Float'中定义
实例实数双 - 在'GHC.Float'中定义

积分

积分,你知道, ..., - 2,-1,0,1,... 。类型,如Integer(又名大int),Int,Int64等是实例。

 :info Integral 
class (Real a,Enum a)=> Integral a where
quot :: a - > a - > a
rem :: a - > a - >
div :: a - > a - > a
mod :: a - > a - > a
quotRem :: a - > a - > (a,a)
divMod :: a - > a - > (a,a)
toInteger :: a - >整数
- 定义于'GHC.Real'
实例积分词 - 定义于'GHC.Real'
实例积分整数 - 定义于'GHC.Real'
实例Integral Int - 定义在'GHC.Real'中

整数



类型,而不是类型类,例如我们迄今为止讨论过的类型类,它可以表示无界整数。所以 2 ^ 3028 是一个合法的值。



Int



固定宽度积分。在GHC编译器中,这取决于你的体系结构是32位还是64位。 Haskell语言只保证至少有29位。



比率

这是一个类型构造函数,所以你可以像 Ratio Integer 那样获取两个整数的比率类型(数学上的 a / b
理性

两个整数的比例,理解比率,你很好:

 :i Rational 
类型Rational = Ratio Integer

Double

用于双精度浮点值的类型。



浮点型

A键入单精度浮点值。


I would like a clear explanation of Num, Real, Integral, Integer, Int, Ratio, Rational, Double, Float.

解决方案

This answer mostly assumes you know the difference between types and type classes. If that difference is hazy to you then clear up your understanding there before reading on.

Num

Num is a typeclass that includes all numeric types.

:info Num
class Num a where
  (+) :: a -> a -> a
  (-) :: a -> a -> a
  (*) :: a -> a -> a
  negate :: a -> a
  abs :: a -> a
  signum :: a -> a
  fromInteger :: Integer -> a
        -- Defined in ‘GHC.Num’
instance Num Word -- Defined in ‘GHC.Num’
instance Num Integer -- Defined in ‘GHC.Num’
instance Num Int -- Defined in ‘GHC.Num’
instance Num Float -- Defined in ‘GHC.Float’
instance Num Double -- Defined in ‘GHC.Float’

Real

Also a typeclass covering those types that can be represented as a real value (the Rational type).

:info Real
class (Num a, Ord a) => Real a where
  toRational :: a -> Rational
        -- Defined in ‘GHC.Real’
instance Real Word -- Defined in ‘GHC.Real’
instance Real Integer -- Defined in ‘GHC.Real’
instance Real Int -- Defined in ‘GHC.Real’
instance Real Float -- Defined in ‘GHC.Float’
instance Real Double -- Defined in ‘GHC.Float’

Integral

A type class for integrals, you know, ...,-2,-1,0,1,.... Types such as Integer (aka big int), Int, Int64, etc are instances.

:info Integral
class (Real a, Enum a) => Integral a where
  quot :: a -> a -> a
  rem :: a -> a -> a
  div :: a -> a -> a
  mod :: a -> a -> a
  quotRem :: a -> a -> (a, a)
  divMod :: a -> a -> (a, a)
  toInteger :: a -> Integer
        -- Defined in ‘GHC.Real’
instance Integral Word -- Defined in ‘GHC.Real’
instance Integral Integer -- Defined in ‘GHC.Real’
instance Integral Int -- Defined in ‘GHC.Real’

Integer

A type, not a type class such as what we've talked about till now, that can represent unbounded integers. So 2^3028 is a legal value.

Int

A fixed-width integral. In the GHC compiler this is 32 or 64 bits depending on your architecture. The Haskell language only guarantees this will be at least 29 bits.

Ratio

This is a type constructor, so you would say something like Ratio Integer to get a type for ratio's of two integers (mathematically a/b).

Rational

Well a rational is literally a ratio of two integers, understand ratio and you're good:

:i Rational
type Rational = Ratio Integer

Double

A type for double precision floating point values.

Float

A type for single precision floating point values.

这篇关于Haskell中的数字解释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:16