这个问题在这里已经有了答案:
什么=>在类型签名中意味着什么? 3个答案我刚开始阅读通过学习你一个好主意的哈斯克尔。 在第3章中,我看到作者使用=>但我无法确切地知道它的作用以及它们使用它的原因。
我认为这是第一次使用它:
lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"这是另一个例子:
tell :: (Show a) => [a] -> String tell [] = "The list is empty" tell (x:[]) = "The list has one element: " ++ show x tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y tell (x:y:_) = "This list is long. The first two elements are: " ++ show x ++ " and " ++ show y谢谢。
This question already has an answer here:
What does => mean in a type signature? 3 answersI've just started reading through Learn You a Haskell for Great Good. In chapter 3, I've seen the author use => but I could not find out exactly what it does and why they use it.
I think this is the first time it was used:
lucky :: (Integral a) => a -> String lucky 7 = "LUCKY NUMBER SEVEN!" lucky x = "Sorry, you're out of luck, pal!"Here is another example:
tell :: (Show a) => [a] -> String tell [] = "The list is empty" tell (x:[]) = "The list has one element: " ++ show x tell (x:y:[]) = "The list has two elements: " ++ show x ++ " and " ++ show y tell (x:y:_) = "This list is long. The first two elements are: " ++ show x ++ " and " ++ show yThanks.
最满意答案
总之 :
虽然不完整,但如果您尚未阅读关于class es的内容,则可以将其视为一种约束,即在类型a上定义了一组函数。
说明 :
你可以看到它将限制放在一个类型上。 例如在lucky的情况下:
lucky :: (Integral a) => a -> String这意味着函数是为任何类型a定义a ,只要a是Integral的instance 。
现在我不知道你是否熟悉class es(那些不是面向对象编程中使用的那些),但类或多或少地引入了函数,然后可以在函数定义中使用。 例如,类Integral定义为:
class (Real a, Enum a) => Integral a where quot :: a -> a -> a rem :: a -> a -> a div :: a -> a -> a mod :: a -> a -> a quotRem :: a -> a -> (a, a) divMod :: a -> a -> (a, a) toInteger :: a -> Integer现在这个类很难理解,因为它在签名中也使用了=> 。 显示可能更简单:
class Show a where showsPrec :: Int -> a -> ShowS show :: a -> String showList :: [a] -> ShowS这意味着如果a是Show的实例,则可以使用show函数 。 否则,以下行:
tell (x:[]) = "The list has one element: " ++ show x不会编译,因为Haskell不确定它可以调用x show 。 然而,通过限制类型a ,我们知道我们只能使用tell类型,其中a是Show一个实例,因此show函数被定义。
In short:
Although not complete, if you haven't yet read about classes, you can see it as a constraint that there is defined a set of functions on the type a.
Explanation:
You can see it as putting constraints on a type. For instance in the case of lucky:
lucky :: (Integral a) => a -> StringIt means the function is defined for any type a, as long as a is an instance of Integral.
Now I don't know if you are familiar with classes yet (those are not the ones used in object-oriented programming), but classes more or less introduce functions on a that can then by used in your function definition. For instance the class Integral is defined as:
class (Real a, Enum a) => Integral a where quot :: a -> a -> a rem :: a -> a -> a div :: a -> a -> a mod :: a -> a -> a quotRem :: a -> a -> (a, a) divMod :: a -> a -> (a, a) toInteger :: a -> IntegerNow this class is rather hard to understand since it uses => in the signature as well. Perhaps an easier one is Show:
class Show a where showsPrec :: Int -> a -> ShowS show :: a -> String showList :: [a] -> ShowSIt means that if a is an instance of Show, that you can use the show function on a. Otherwise the following line:
tell (x:[]) = "The list has one element: " ++ show xwould not compile since Haskell isn't sure it can call show on x. By constraining the type a however, we know that we can only use tell on types where a is an instance of Show, and thus the show function is defined.
更多推荐
发布评论