我想添加另一个条件来删除已经喜欢电影的用户 [英] I want to add another condition which removes a user that has already liked a film

查看:105
本文介绍了我想添加另一个条件来删除已经喜欢电影的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个让用户不喜欢电影的功能。然而,用户只能存在于喜欢/不喜欢的列表中,而不能同时存在

  addUserDislikes :: Title  - >用户 - > [电影]  - > [电影] 
addUserDislikes标题用户db = [if ti == title&& elem用户不喜欢==假然后(ti,di,yr,like,dislike ++ [user])else(ti,di,yr,like,dislike)| (ti,di,yr,like,dislike)< - db]

用户列表不喜欢,但不会从列表中删除它喜欢我怎么做?



其他信息 - 标题& 用户字符串 Like 不喜欢是元组影片中的列表

解决方案

尝试在列表理解中实现这一点并不是最简单或更可读的方法。相反,请考虑定义一个处理单个 Film 的函数。如果你有这样的功能,整体功能就变成了一张简单的地图:

  addUserDislikes :: Title  - >用户 - > [电影]  - > [电影] 
addUserDislikes title用户= fmap $ dislikeFilm标题用户

您可以定义 dislikeFilm 作为独立函数,或者使用 let 来定义它,其中在 addUserDislikes 中。如果您将函数编写为独立函数,您可以这样写:

 不喜欢电影标题用户影片@(ti ,di,yr,likes,dislikes)= 
if ti == title
then(ti,di,yr,delete user likes,user:dislikes)
else film

请注意,如果未经修改,该函数将返回 film > title 不匹配。另一方面,如果标题匹配,它 用户不喜欢,同时删除喜欢。为此,它从 Data.List 中使用 delete



<这里有一些例子:

  * Q49336125> addUserDislikes2010foo[] 
[]

* Q49336125> addUserDislikes2010foo[(2010,Peter Hyams,1984,[],[])]
[(2010,Peter Hyams,1984,[],[foo ])]

* Q49336125> addUserDislikes2010foo[(2010,Peter Hyams,1984,[foo],[])]
[(2010,Peter Hyams,1984,[], [foo]]]

* Q49336125> addUserDislikes2010foo[
(2010,Peter Hyams,1984,[foo],[bar]),
(2001Stanley Kubrick ,1968,[baz,qux],[])]
[(2010,Peter Hyams,1984,[],[foo,bar]),
(2001,Stanley Kubrick,1968,[baz,qux],[])]

(最后评估由我手动编辑,使其更具可读性。)



(PS我喜欢 2001 2010 ,但我想使用一些短片标题。)


I am trying to create a function which allows a user to dislike a film. However the user can only exist in the list of likes/dislikes but not both

 addUserDislikes :: Title -> User -> [Film] -> [Film] 
 addUserDislikes title user db = [ if ti == title && elem user dislike == False then (ti, di, yr,like,dislike++ [user])else (ti, di, yr, like, dislike) | (ti, di, yr, like, dislike) <- db] 

This function only adds the user to the list of Dislikes but does not remove it from list of likes how do I do this?

other info - The Title & User are Strings. Like and Dislikes are lists in the tuple Film.

解决方案

Trying to accomplish this in a list comprehension is hardly the easiest, or more readable approach. Instead, consider defining a function that handles a single Film. If you have such a function, the overall function becomes a simple map:

addUserDislikes :: Title -> User -> [Film] -> [Film]
addUserDislikes title user = fmap $ dislikeFilm title user

You can define dislikeFilm as a stand-alone function, if you will, or alternatively define it using let or where in addUserDislikes. If you write the function as a stand-alone function, you could write it like this:

dislikeFilm title user film@(ti, di, yr, likes, dislikes) =
  if ti == title
    then (ti, di, yr, delete user likes, user : dislikes)
    else film

Notice that this function returns film unmodified if title doesn't match. If, on the other hand, the title matches, it cons user onto dislikes, and simultaneously removes if from likes. For that, it uses delete from Data.List.

Here's some examples:

*Q49336125> addUserDislikes "2010" "foo" []
[]

*Q49336125> addUserDislikes "2010" "foo" [("2010", "Peter Hyams", 1984, [], [])]
[("2010","Peter Hyams",1984,[],["foo"])]

*Q49336125> addUserDislikes "2010" "foo" [("2010", "Peter Hyams", 1984, ["foo"], [])]
[("2010","Peter Hyams",1984,[],["foo"])]

*Q49336125> addUserDislikes "2010" "foo" [
    ("2010", "Peter Hyams", 1984, ["foo"], ["bar"]),
    ("2001", "Stanley Kubrick", 1968, ["baz", "qux"], [])]
[("2010","Peter Hyams",1984,[],["foo","bar"]),
 ("2001","Stanley Kubrick",1968,["baz","qux"],[])]

(Last evaluation hand-edited by me to make it more readable.)

(P.S. I liked both 2001 and 2010, but I wanted to use some short film titles.)

这篇关于我想添加另一个条件来删除已经喜欢电影的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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