Purescript复数零元素(Purescript示例练习)

huangapple go评论49阅读模式
英文:

Purescript Complex zero element (Purescipt by example exercies)

问题

I solve Purescript exercise from the book https://book.purescript.org/chapter6.html#multi-parameter-type-classes

I don't understand what the "zero" statement is?

I have a "Complex" type, and I should define a Semiring Complex instance of it.

newtype Complex
= Complex
 { real :: Number
 , imaginary :: Number
 }

A simple solution to define the zero complex element (0.0+0.0i) is:

instance semiringComplex :: Semiring Complex where
  zero = Complex {real: 0.0, imaginary: 0.0}

But another solution presented in this book is:

instance semiringComplex :: Semiring Complex where
 zero = Complex zero

I do not understand how does this statement work ("Complex zero"), if zero is defined as a function (not as a Complex type element).

> :t zero
forall (a :: Type). Semiring a => a

And what I found tricky is:

cz = Complex zero 
> cz
0.0+0.0i

How does PSCI understand that zero is 0.0+0.0i, but I did not define 0.0 constants?

英文:

I solve Purescript exercise from the book https://book.purescript.org/chapter6.html#multi-parameter-type-classes

I don't understand what "zero" statement is?

I have "Complex" type, and should define Semiring Complex instance of it.

newtype Complex
= Complex
 { real :: Number
 , imaginary :: Number
 }

Simple solution to define zero complex element (0.0+0.0i) as

instance semiringComplex :: Semiring Complex where
  zero = Complex {real: 0.0, imaginary: 0.0}

But another solution, presented in this book is

instance semiringComplex :: Semiring Complex where
 zero = Complex zero

I do not understand, how does this statement work ("Complex zero"), if zero defined as function (not as a Complex type element)

> :t zero
forall (a :: Type). Semiring a => a

And what I found tricky is

cz = Complex zero 
> cz
0.0+0.0i

How does PSCI understand that zero is 0.0+0.0i, but I did not define 0.0 constants?

答案1

得分: 1

zeroSemiring类的一个方法。因此,它适用于该类的任何具有实例的类型。

恰巧,对于记录(Record r存在Semiring类的一个实例,也就是说,对于记录类型,只要记录的所有字段类型都有Semiring实例。这个实例的定义方式是,zero是一个所有字段都设置为它们各自的zero值的记录。

因此,例如:

(zero :: { x :: Int, y :: Unit }) == { x: 0, y: unit }

因为(zero :: Int) == 0(zero :: Unit) == unit

或者,在您的特定情况下:

(zero :: { real :: Number, imaginary :: Number }) == { real: 0.0, imaginary: 0.0 }

因为(zero :: Number) == 0.0

这意味着编写Complex zero等同于编写Complex { real: 0.0, imaginary: 0.0 }

英文:

zero is a method of the Semiring class. So it works for any type for which there is an instance of that class.

It just so happens that there is an instance of the Semiring class for Record r - that is, for records. As long as there are Semiring instances for all the record's field types. And the way that instance is defined is that zero is a record with all fields set to their respective zero values.

So, for example:

(zero :: { x :: Int, y :: Unit }) == { x: 0, y: unit }

Because (zero :: Int) == 0 and (zero :: Unit) == unit

Or, in your particular case:

(zero :: { real :: Number, imaginary :: Number }) == { real: 0.0, imaginary: 0.0 }

Because (zero :: Number) == 0.0

Which means that writing Complex zero is equivalent to writing Complex { real: 0.0, imaginary: 0.0 }

huangapple
  • 本文由 发表于 2023年5月22日 19:30:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定