简体   繁体   English

function 类型声明错误

[英]Wrong function type declaration

I have the following function that sholud return only odd numbers from the List我有以下 function 应该只返回列表中的奇数

oddsOnly :: Integral a => [a] -> [a]
oddsOnly [] = []
oddsOnly (x:xs)
 |odd x = x : oddsOnly xs
 |otherwise = oddsOnly xs

My question is about the aim of using我的问题是关于使用的目的

Integral a =>积分 a =>

Actually it is not posssible to implement such function with such type declaration实际上不可能用这样的类型声明来实现这样的 function

oddsOnly:: [a] -> [a]仅赔率:: [a] -> [a]

As far as I know even and odd functions are in standart Prelude library so why simplified daclaration does not work?据我所知,偶数奇数函数在标准Prelude库中,那么为什么简化的声明不起作用?

Thanks in advance提前致谢

why simplified daclaration does not work?为什么简化的声明不起作用?

Because you can't tell if a string is odd or not.因为你无法判断一个字符串是不是奇数。 Or if a float is odd or not.或者浮点数是否奇数。 In general, it's not possible to tell if a value of any type a is odd or not because the notion of being odd may not be defined for this type.通常,无法判断任何类型a的值是否为奇数,因为可能没有为该类型定义奇数的概念。

It is defined, however, for Integral types, so you must specify that a is Integral .但是,它Integral类型定义的,因此您必须指定aIntegral

In terms of Haskell, odd works on Integral s only:就 Haskell 而言, odd仅适用于Integral s:

Prelude> :t odd
odd :: Integral a => a -> Bool

Thus, you must pass it a value of a type that is Integral .因此,您必须向它传递一个Integral类型的值。

The signature of odd and even is odd:: Integral a => a -> Bool and even:: Integral a => a -> Bool respectively. oddeven的签名分别是odd:: Integral a => a -> Booleven:: Integral a => a -> Bool So these contain an Integral a type constraint [Haskell-wiki] .所以这些包含Integral a类型约束 [Haskell-wiki]

A type constraint has nothing to do from where it is defined.类型约束与其定义的位置无关。 It specifies that the function only is defined for a subset of types.它指定 function 仅为类型的子集定义。 Only for types that are members of the Integral typeclass , you can use the function. Integral types are types like that specify a subset of ℤ, so Int , Int64 , Word8 , Integer , … Types.仅对于作为Integral类型类成员的类型,您可以使用 function。Integral 类型是指定 ℤ 子集的Integer ,因此IntInt64Word8 、... 类型。 odd and even do not make much sense over Float s, String s, Char s, etc. What would that mean? oddevenFloat s、 String s、 Char s 等没有多大意义。那是什么意思?

By thus specifying the Integral a => type constraint, you specify that this function can only work with integer-like items.通过这样指定Integral a =>类型约束,您可以指定此 function 只能与类似整数的项一起使用。

Note that you can use filter:: (a -> Bool) -> [a] -> [a] here to retain only odd numbers:请注意,您可以在此处使用filter:: (a -> Bool) -> [a] -> [a]以仅保留奇数:

oddsOnly :: Integral a => [a] -> [a]
oddsOnly = filter odd

Let's look at the type signature of odd : odd:: Integral a => a -> Bool .让我们看看odd的类型签名: odd:: Integral a => a -> Bool So to use odd with a parameter, that parameter needs to be Integral .因此,要将odd与参数一起使用,该参数必须是Integral This makes sense intuitively: as @ForceBru said in another answer, you can't tell whether eg a float such as 1.5 or a string such as "hello" is odd, as the notion of oddness is only defined for integral values.这在直觉上是有道理的:正如@ForceBru 在另一个答案中所说,您无法判断例如1.5之类的浮点数或诸如"hello"类的字符串是否是奇数,因为奇数的概念仅针对整数值定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM