本文介绍了如何否定列表中除特定数字之外的每个数字?哈斯克尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对haskell还是比较陌生,并且知道我们可以使用否定符来否定列表.否定运算不会否定float,int和integer类型的常规列表中的数字零.但是,如果您有其他数据类型的列表怎么办?如果我否定其他数据类型,那么该列表中的数字零也将被否定.有没有一种方法可以不否定列表中的0和0.0之类的数字?

I'm relatively new to haskell, and know that we can use the negation to negate a list. The negation does not negate the number zero in a regular list of type float, int, and integer. But what if you had a list of a different data type? If I negate a different data type, then the number zero in that list will also be negated. Is there a way to not negate numbers like 0 and 0.0 in the list?

推荐答案

您说

但是这个断言是不正确的.参见:

but this assertion is incorrect. See:

> negate 0 :: Float
-0.0
> negate 0 :: Double
-0.0
> map negate [0] :: [Float]
[-0.0]

此代码其余部分的行为直接源自此事实.为了进一步阅读,我强烈建议每个计算机科学家应该了解的有关浮动的知识-点算术,其中深入讨论了为什么浮点必须具有一个不同于零的负零.但是简短的版本是第201页的这句话:

The behavior of the rest of your code follows directly from this fact. For further reading I highly recommend What Every Computer Scientist Should Know About Floating-Point Arithmetic, which includes an in-depth discussion of why floating point must have a negative zero distinct from zero. But the short version is this sentence from page 201:

这篇关于如何否定列表中除特定数字之外的每个数字?哈斯克尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 05:56