簡體   English   中英

Haskell 如何在元組列表中獲取兩種不同數據類型之間的差異?

[英]Haskell How to get the difference between 2 different data types in a list of tuples?

我想知道是否有一種簡單的方法可以在元組列表中獲取 Haskell 中兩種數據類型之間的差異。

假設我定義了 2 種數據類型:

type Name = String
type Age = String
type Color = String
type Size = Int 

data Animal = Animal Name Age Color Size
data Cat = Cat Name Age Color

然后,我想得到一個包含除貓以外的所有動物的元組列表。 該功能將類似於:

getAnimalsButCats :: [Animal] -> [Cat] -> [(Name, Age, Color)]
getAnimalsButCats [(Animal name age color _)] [(Cat nameC ageC colorC)] = //diff between [Animal] and [Cat]

你可能想要類似的東西

type Name = String
type Age = String
type Color = String
type Size = Int

data AnimalKind = Cat | Dog | Horse | Bird
data Animal = Animal AnimalKind Name Age Color Size

getAnimalsButCats :: [Animal] -> [(Name, Age, Color)]
getAnimalsButCats [] = []
-- Skip cats
getAnimalsButCats ((Animal Cat _ _ _ _):xs) = getAnimalsButCats xs
-- Save name, age, color for any other animal
getAnimalsButCats ((Animal _ name age color _): xs) = (name, age, color) : (getAnimalsButCats xs)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM