Haskell 列表理解,从数字列表中删除整数
Haskell List Comprehension, delete integers from List of numbers
我想在列表理解中实现一个功能。
它应该删除数字列表中的整数。
我对此有疑问。
delete xs = [ys|ys<-xs, ys /=fromInteger (round ys) ]
xxx.hs> delete [1,2,3.0,4.5,6.7]
[4.5,6.7]
这是否意味着 3.0 被算作整数而不是浮点数?
还有一个问题:
delete xs = [ys|ys<-xs, ys ==fromInteger (round ys) ]
这次我希望它是数字列表中的 return 个整数。
xxx.hs> delete [1,2,3.0,4.5,6.7]
[1.0,2.0,3.0]
既然我没有给出小数形式的数字 1 和 2,为什么它 return 是小数形式的数字?
谢谢你帮助我。
I want to implement a function in list comprehension. It should deletes the integers in a list of numbers.
列表中的所有元素都具有相同的类型。所以在列表 [1.2, 3, 4.5]
中,3
也是属于 Floating
类型类成员的类型的值。
Since i did not give the number 1
and 2
in decimal form, why it returns the numbers in decimal?
因为所有元素都是同一类型。 GHC 将默认为 Double
type defaulting rules。
您的过滤器未指定元素应为 Integral
类型。这也将是 non-sensical 因为类型是在编译时解析的,而不是在运行时。如果你 fromInteger (round ys)
,它只是检查 ys
是否相同。由于round 3.0 == 3
,在本例中为fromInteger 3 == 3.0
,因此它过滤掉了具有小数部分的元素。 1.0
和 2.0
没有小数部分。
然而过滤并不安全,对于较大的数字,尾数不能准确表示每个整数,因此这意味着当这些不是整数时,将过滤保留一些值。有关详细信息,请参阅 is floating point math broken。
我想在列表理解中实现一个功能。 它应该删除数字列表中的整数。
我对此有疑问。
delete xs = [ys|ys<-xs, ys /=fromInteger (round ys) ]
xxx.hs> delete [1,2,3.0,4.5,6.7]
[4.5,6.7]
这是否意味着 3.0 被算作整数而不是浮点数?
还有一个问题:
delete xs = [ys|ys<-xs, ys ==fromInteger (round ys) ]
这次我希望它是数字列表中的 return 个整数。
xxx.hs> delete [1,2,3.0,4.5,6.7]
[1.0,2.0,3.0]
既然我没有给出小数形式的数字 1 和 2,为什么它 return 是小数形式的数字?
谢谢你帮助我。
I want to implement a function in list comprehension. It should deletes the integers in a list of numbers.
列表中的所有元素都具有相同的类型。所以在列表 [1.2, 3, 4.5]
中,3
也是属于 Floating
类型类成员的类型的值。
Since i did not give the number
1
and2
in decimal form, why it returns the numbers in decimal?
因为所有元素都是同一类型。 GHC 将默认为 Double
type defaulting rules。
您的过滤器未指定元素应为 Integral
类型。这也将是 non-sensical 因为类型是在编译时解析的,而不是在运行时。如果你 fromInteger (round ys)
,它只是检查 ys
是否相同。由于round 3.0 == 3
,在本例中为fromInteger 3 == 3.0
,因此它过滤掉了具有小数部分的元素。 1.0
和 2.0
没有小数部分。
然而过滤并不安全,对于较大的数字,尾数不能准确表示每个整数,因此这意味着当这些不是整数时,将过滤保留一些值。有关详细信息,请参阅 is floating point math broken。