简体   繁体   English

如何使用haskell类型?

[英]How to use haskell types?

Really simple question, i can't for the life of me google the info :( 一个非常简单的问题,我不能为我一生使用Google信息:(

I have the following types: 我有以下几种类型:

type NI = Int
type Age = Int
type Balance = Int
type Person = (NI, Age, Balance)

How do I create a function that returns true if a person's age is over 65? 如果一个人的年龄超过65岁,如何创建一个返回true的函数? I've tried: 我试过了:

retired :: Person -> Bool
retired p = p >= 65

It doesn't work obviously, i realise that even when i tried it. 它显然不起作用,我意识到即使尝试了它。 I'm stumped on something so simple. 我对如此简单的事情感到困惑。

as Person is a tuple you should pattern-match it like this: 由于Person是一个元组,因此您应该像这样对它进行模式匹配:

retired (_,age,_) = age >= 65

and it should work 它应该工作

type X = Y defines a type alias . type X = Y定义类型别名 In this example, X is defined to be the same as Y . 在此示例中, X定义为与Y相同。 Therefore, retired :: Person -> Bool is the same as retired :: (Int, Int, Int) -> Bool . 因此, retired :: Person -> Boolretired :: (Int, Int, Int) -> Bool

Then we can ask the question: how do we obtain a person's age? 然后我们可以问一个问题:我们如何获得一个人的年龄? Well, Person is the same as (Int, Int, Int) , and we know the second element of the tuple is the person's age. 好吧, Person(Int, Int, Int) ,我们知道元组的第二个元素是人的年龄。 Therefore, an equivalent question is: how do we obtain the second element of a 3-tuple? 因此,一个等效的问题是:如何获得三元组的第二个元素?

This is done in Haskell by deconstructing the tuple, which is the opposite of constructing the tuple. 在Haskell中,这是通过解构元组来完成的,这与构造元组相反。 An example of constructing a tuple is x = (1,2,3) . 构造元组的一个示例是x = (1,2,3) To deconstruct a tuple, we use a similar notation but flip the sides: (a,b,c) = x . 为了解构元组,我们使用类似的表示法,但反过来: (a,b,c) = x This is the same as (a,b,c) = (1,2,3) , and the same as the three assignments a = 1; b = 2; c = 3 这与(a,b,c) = (1,2,3)相同,并且与三个赋值a = 1; b = 2; c = 3 a = 1; b = 2; c = 3 a = 1; b = 2; c = 3 . a = 1; b = 2; c = 3

Haskell allows you to use this deconstruction notation for function arguments. Haskell允许您将此解构符号用于函数参数。 Therefore, retired p = e could be written retired (ni, age, balance) = e , recalling that Person is a 3-tuple. 因此, retired p = e可以写成retired (ni, age, balance) = e ,回想起Person是一个三元组。 Now it is straight-forward that e should be age >= 65 . 现在很简单, e应该是age >= 65

To elaborate further, retired (ni, age, balance) = e is equivalent to retired p = let (ni, age, balance) = p in e . 为了进一步详细说明, retired (ni, age, balance) = e等于retired p = let (ni, age, balance) = p in e This is useful to know because then function application is clearer. 知道这很有用,因为这样功能应用程序会更清晰。 retired x is let (ni, age, balance) = x in [x/p]e where [x/p]e means "substitute x for p in e ". retired x成为let (ni, age, balance) = x in [x/p]e中的[x/p]e ,其中[x/p]e表示“用x代替e中的 p ”。


Another approach is to define a data type using record notation. 另一种方法是使用记录符号定义数据类型。

data Person = Person { ni :: Int, age :: Int, balance :: Int }

This defines a new type called Person , and is not the same as the 3-tuple of type (Int, Int, Int) . 这定义了一个称为Person的新类型,并且与类型(Int, Int, Int)组不同。 Additionally, this defines three projection functions ni :: Person -> Int , age :: Person -> Int , and balance :: Person -> Int . 另外,它定义了三个投影函数ni :: Person -> Intage :: Person -> Intbalance :: Person -> Int

Now if we implement retired :: Person -> Bool we can do so as retired p = age p >= 65 , or in point-free form retired = (>= 65) . age 现在,如果我们实现retired :: Person -> Bool我们可以做到: retired p = age p >= 65 ,或采用无点形式retired = (>= 65) . age retired = (>= 65) . age . retired = (>= 65) . age

To reconnect with the first approach, can you also use deconstruction? 要重新连接第一种方法,还可以使用解构吗? Absolutely. 绝对。 A Person is constructed as x = Person mnp , and so similarly can be deconstructed as Person abc = x . 将Person构造为x = Person mnp ,因此类似地可以将其构造为Person abc = x Therefore, another definition for retired is retired (Person nab) = a >= 65 . 因此, retired另一个定义是retired (Person nab) = a >= 65

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

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