英文:
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
zero
是Semiring
类的一个方法。因此,它适用于该类的任何具有实例的类型。
恰巧,对于记录(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 }
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论