[英]remove type from type list haskell
So i'm new to Haskell, and I was wondering if anyone could help me. 所以我是Haskell的新手,我想知道是否有人可以帮助我。
I've got a list of a custom data type like so: 我有一个自定义数据类型的列表,如下所示:
type Title = String
type Manager = String
type Year = Int
type Fan = String
type Album = (Title, Manager, Year, [Fan])
And I have a pre-made static database of albums 我有一个预制的静态数据库专辑
albumDatabase :: [Album]
albumDatabase = [(...)]
And I have a function that returns all albums that a manager has made: 我有一个函数可以返回经理所做的所有专辑:
manAlbum :: String -> [Album] -> [Album]
manAlbum d database = filter ((\(_,album,_,_) -> d == album)) database
My question is, from this new list of all the managers albums, I need to retrieve only the fans and drop the Title, Manager and Year. 我的问题是,从所有管理员专辑的新列表中,我只需要检索粉丝并删除标题,经理和年份。 However I am unsure of how to tell haskell that I only want this field from the custom datatype to be returned. 但是我不确定如何告诉haskell我只想要返回自定义数据类型的这个字段。
As 4castle mentioned, you can use map
: 正如4castle所提到的,你可以使用map
:
getAlbumFans :: [Album] -> [[Fan]]
getAlbumFans database = map (\(_,_,_,fans) -> fans) database
Also you could make the manAlbum
function more readable by giving it a more descriptive name like getAlbumsByManager
and replacing String
in the type signature with Manager
. 您还可以通过为其提供更具描述性的名称(如getAlbumsByManager
并使用Manager
替换类型签名中的String
,使manAlbum
函数更具可读性。
Another solution might be to make Album
an algebraic data type. 另一种解决方案可能是使Album
成为代数数据类型。 I guess that the code might be easier to understand, when rewritten as follows: 我猜这段代码可能更容易理解,重写如下:
type Title = String
type Manager = String
type Year = Int
type Fan = String
data Album = Album { title :: Title, manager :: Manager, year :: Year, fans :: [Fan]} deriving (Show)
albumDatabase :: [Album]
albumDatabase = [Album {title="A", manager="B", year=5, fans=["a","b"]}]
check :: Album -> String -> Bool
check a d=title a==d
manAlbum :: String -> [Album] -> [[Fan]]
manAlbum d database = map (\a->fans a) $ filter (\a->title a==d) database
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.