输出自定义数据类型列表的内容 [英] Outputting the contents of a list of a custom data type

查看:117
本文介绍了输出自定义数据类型列表的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义数据类型 Movie = String Int [(String,Int)] (Movie Name Year [(Fan,Rating)]并且想要做一对情侣首先,我想创建一个函数,从元组列表中平均Ints,并输出这个数字。到目前为止,我有这个不完整的函数:

p>

avgRating :: [DataType] - > Int
avgRating [(Movie ab [(fan,rating)])] = sumRatings / (length [< mylist>])



这里我需要一个函数sumRatings递归遍历列表并总计所有评级,但是我我不知道该从哪里开始。



我在这里遇到的另一个问题是,我不知道要放哪里< mylist> 就像我通常给列表一个变量名然后在那里使用它一样,但是因为我已经把列表拆分了来定义其他变量,所以我不能命名它。



我希望有道理,谢谢。

数据结构定义为

  data Movie = Movie String Int [(String,Int)] 

虽然这可行,但当您拥有多个字段时,处理起来可能有点麻烦。相反,您可以使用类型别名和记录语法作为

  type Name = String 
type Year = Int
类型Rating = Int

data Movie = Movie
{mName :: Name
,mYear :: Year
,mRatings :: [(Name,Rating)]
}派生(Eq,Show)

现在事情更简洁明了,与...合作。 mName mYear mRatings 函数将需要电影并返回相应的字段。您的 Movie 构造函数仍然以相同的方式工作,所以它不会破坏现有的代码。



要计算评分的平均值,你真的需要一个函数来提取电影的所有评级并将它们聚合成一个列表:

 评分::电影 - > [评分] 
评分mov = map snd $ mRatings mov

然后你只需要一个平均值函数。这会有点不同,因为你不能直接计算 Int s的平均值,你必须转换为浮点类型:

  average:[评分]  - >浮点数 - 双精度在这里并不是真的需要
平均值rs = fromIntegral(sum rs)/ fromIntegral(length rs)

fromIntegral 函数将 Int 转换为 Float (实际的类型签名更普遍一些)。既然 Int s的 sum 是一个 Int 并且列表的长度总是一个 Int ,您需要转换两者。



现在您可以将它们组合到一个函数中:

  movieAvgRating :: Movie  - >浮动
movieAvgRating =平均值。评分

现在,如果您需要计算多部电影的平均评分,则可以应用 ratings 给它们中的每一个,将它们汇总到一个评级列表中,然后调用 average 。我会建议看看 concatMap 函数。你会想要制作一个像

  moviesAvgRating :: [Movie]  - >浮动
moviesAvgRating movs =平均$ ???


I have a custom data type Movie = String Int [(String,Int)] (Movie Name Year [(Fan,Rating)] and want to do a couple of things:

First I want to make a function that averages the Ints from the list of tuples and just outputs that number. So far I have this incomplete function:

avgRating :: [DataType] -> Int avgRating [(Movie a b [(fan,rating)])] = sumRatings / (length [<mylist>])

Here I need a function sumRatings to recurse through the list and sum all the ratings, but i'm not sure where to start.

The other issue I have here is that i'm not sure what to put where <mylist> is as I would normally give the list a variable name and then use it there, but since I have split the list up to define other variables I can't name it.

I hope that makes sense, thanks.

解决方案

I'm guessing you have a data structure defined as

data Movie = Movie String Int [(String, Int)]

While this works, it can be a bit cumbersome to work with when you have that many fields. Instead, you can leverage type aliases and record syntax as

type Name = String
type Year = Int
type Rating = Int

data Movie = Movie
    { mName :: Name
    , mYear :: Year
    , mRatings :: [(Name, Rating)]
    } deriving (Eq, Show)

Now things are a bit more explicit and easier to work with. The mName, mYear, and mRatings functions will take a Movie and return the corresponding field from it. Your Movie constructor still works in the same way too, so it won't break existing code.

To calculate the average of the ratings, you really want a function that extracts all the ratings for a movie and aggregates them into a list:

ratings :: Movie -> [Rating]
ratings mov = map snd $ mRatings mov

Then you just need an average function. This will be a bit different because you can't calculate the average of Ints directly, you'll have to convert to a floating point type:

average :: [Rating] -> Float   -- Double precision isn't really needed here
average rs = fromIntegral (sum rs) / fromIntegral (length rs)

The fromIntegral function converts an Int to a Float (the actual type signature is a bit more general). Since both the sum of Ints is an Int and the length of a list is always an Int, you need to convert both.

Now you can just compose these into a single function:

movieAvgRating :: Movie -> Float
movieAvgRating = average . ratings

Now, if you need to calculate the average ratings for several movies, you can apply ratings to each of them, aggregate them into a single list of ratings, then call average on that. I would suggest looking at the concatMap function. You'll be wanting to make a function like

moviesAvgRating :: [Movie] -> Float
moviesAvgRating movs = average $ ???

这篇关于输出自定义数据类型列表的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆