简体   繁体   English

Haskell:了解自定义数据类型

[英]Haskell: Understanding custom data types

I am trying to make my own custom data type in Haskell. 我正在尝试在Haskell中创建自己的自定义数据类型。

I have the following data types: 我有以下数据类型:

type Length = Integer 
type Rotation = Integer 
data Colour = Colour { red, green, blue, alpha :: Int }
            deriving (Show, Eq)

I am trying to make a custom data type that can be either one of the data types above. 我正在尝试制作一个可以是上述数据类型之一的自定义数据类型。 I have the following: 我有以下几点:

data Special 
  = L Length 
  | R Rotation 
  | Col Colour  
  deriving (Show, Eq) 

However, I would like to be able to extract the Length , Rotation and Colour value if I have an instance of the Special data type. 但是,如果我有Special数据类型的实例,我希望能够提取LengthRotationColour值。

If I had: 如果我有:

L length

Would length here be of type Special or of type Length ? 这里的length可以是Special类型还是Length类型? If length is of type Special is there any way to extract it so it's of type Length ? 如果lengthSpecial类型,则有任何方法可以提取它,因此为Length类型?

For example, is the following code valid? 例如,以下代码有效吗?

takeL (x:xs)
      | x == (L length) = length

Any insights are appreciated. 任何见解都表示赞赏。

For the expression L length to be valid, length would have to be a Length (because L :: Length -> Special ). 为了使表达式L length有效, length必须为Length (因为L :: Length -> Special )。

takeL (x:xs)
      | x == (L length) = length

is not valid. 无效。 Unless you've redefined length somewhere, length is a function [a] -> Int from the standard library, so L length is a type error. 除非您在某处重新定义了length ,否则length是标准库中的函数[a] -> Int ,因此L length是类型错误。

I think what you're trying to do here is just pattern matching: 我认为您要在此处进行的只是模式匹配:

takeL (L length : xs) = length

The data type definition 数据类型定义

data Special  = 

reads: Special is a new type such that to create a value of type Special , 读取: Special是一个新类型 ,可以创建Special类型的

                  L Length 
  • call L l where l is a value of type Length ; 调用L l ,其中lLength类型的值; or 要么

      | R Rotation 
  • call R r where r is a value of type Rotation ; 调用R r ,其中rRotation类型的值; or 要么

      | Col Colour 
  • call Col c where c is a value of type Colour . 调用Col c ,其中cColour类型的值。


To analyze a value of type Special , there are three cases to consider: 要分析Special类型的值,需要考虑以下三种情况:

foo :: Special -> ...
foo val = 
    case val of
        L   l -> ...  
  • l is a value of type Length , in the case val was actually L l ; lLength类型的值,如果val实际上是L l ; or 要么

      R r -> ... 
  • r is a value of type Rotation , in the case val was actually R r ; rRotation类型的值,如果val实际上是R r ; or 要么

      Col c -> ... 
  • c is a value of type Colour , in the case val was actually Col c . cColour类型的值,在val实际上是Col c的情况下。

The case syntax in function definitions can also be expressed with the pattern based clauses: 函数定义中的case语法也可以使用基于模式的子句来表达:

foo :: Special -> ...
foo (L   l) = ...
foo (R   r) = ...
foo (Col c) = ...

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

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