简体   繁体   English

从数据类型Haskell返回值

[英]Returning value from a Datatype Haskell

Lets say I have created the following Datatype that looks like this: 可以说我创建了如下所示的以​​下数据类型:

data MolSeq = DNA (String, String) String | Protein (String, String) String 

Then I create a MolSeq datatype of type DNA like this: 然后,我创建一个DNA类型的MolSeq数据类型,如下所示:

dna = DNA ("T2", "ACATAA") "DNA"

How can I then later in my program return let's say the value "T2"? 以后如何在程序中返回值“ T2”呢?

Thanks! 谢谢!

You can pattern match it, for example: 您可以对其进行模式匹配,例如:

getMolString :: MolSeq -> String
getMolString (DNA     (s, _) _) = s
getMolString (Protein (s, _) _) = s

Here you have a live example 这里有一个现场例子

A data type is just data, you should make functions to work with that, you can't think of them as objects with attributes or methods. 数据类型就是数据,您应该使函数能够使用它,而不能将它们视为具有属性或方法的对象。

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

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