Haskell - 对数据库中的电影进行评级或重新评分

时间:2016-03-06 02:35:08

标签: haskell

我的编码很强大,但出于某些原因swapRate功能,需要一些修复来获得正确的结果。

import Prelude
import Data.Char
import Data.List
import Text.Printf
import Data.Ord

-- Types
type Title = String
type Director = String
type Year = Int
type UserRatings = (String,Int)

-- Define Film type here 
type Film = (Title,Director,Year,[UserRatings])

-- Database Type
type Database = [Film]
testDatabase = [    
("Blade Runner", "Ridley Scott", 1982, [("Amy",5), ("Bill",8), ("Ian",7), ("Kevin",9), ("Emma",4), ("Sam",7), ("Megan",4)]),
("The Fly", "David Cronenberg", 1986, [("Megan",4), ("Fred",7), ("Chris",5), ("Ian",0), ("Amy",6)]),
("Psycho", "Alfred Hitchcock", 1960, [("Bill",4), ("Jo",4), ("Garry",8), ("Kevin",7), ("Olga",8), ("Liz",10), ("Ian",9)]),
("Body Of Lies", "Ridley Scott", 2008, [("Sam",3), ("Neal",7), ("Kevin",2), ("Chris",5), ("Olga",6)]),
("Avatar", "James Cameron", 2009, [("Olga",1), ("Wally",8), ("Megan",9), ("Tim",5), ("Zoe",8), ("Emma",3)]),
("Hugo", "Martin Scorsese", 2011, [("Sam",9), ("Wally",3), ("Zoe",5), ("Liz",7)])
               ]

swapRate :: String -> Int -> String -> Title -> [UserRatings] -> [UserRatings]
swapRate _ _ _ _ [] = []
swapRate user newRate film title ((name, rate):xs)
    | user==name && film==title  = (user,newRate):xs -- Does demo 11
    -- | user/=name && film==title = (user,newRate):xs -- Does demo 1
    | otherwise = (name,rate) : (swapRate user newRate film title xs)
-- * Outputs an updated list

printSwappedFilmRating :: String -> Int -> String -> Film -> String
printSwappedFilmRating user newRate film (t,d,y,u) = "Title: " ++ t ++ "\nDirector: " ++ d ++ "\nYear: " ++ show y ++ "\nUser's Rating: " ++ (show (swapRate user newRate film t u)) ++ "\n" ++ "\n"
-- * Formatting the output of Title, Director, Year and the tuple UserRatings

printSwappedFilmRate :: String -> Int -> String -> Database -> String
printSwappedFilmRate user newRate film database = concat (map (printSwappedFilmRating user newRate film) database)
-- * Applies the printSwappedFilmRating funtion to the database and the list is concatenated as String


----- Main Function -----

rateOrRerate :: String -> Int -> String -> Database -> String
rateOrRerate user newRate film database = printSwappedFilmRate user newRate film database

-- * Receives two Strings and an Int and with the use of printSwappedFilmRate, 
-- it prints all the movies and the updated rating of a particular user with nice formatting    


----- DEMO FUNCTIONS -----
demo :: Int -> IO ()

----- Demo 1 -----
demo 1  = putStrLn (rateOrRerate "Emma" 10 "Hugo" testDatabase)
-- * All films after Emma rates "Hugo" 10

----- Demo 11 -----
demo 11 = putStrLn (rateOrRerate "Emma" 10 "Avatar" testDatabase) 
-- * All films after Emma rates "Avatar" 10

该怎么做:
  - 第一名警卫在“阿凡达”电影中覆盖[“艾玛”,3]至[“艾玛”,10]的比率   - 第二名后卫在“雨果”电影中添加了[“Emma”,10]

所以在swapRate函数中,我无法让两个警卫同时工作。如果我对第一个警卫发表评论,那么第二个警卫会起作用,反之亦然寻找一种让 工作的方法。

1 个答案:

答案 0 :(得分:1)

要摆脱警告,将空字符串""上的模式匹配更改为仅_可以解决checkRateRerateswapRate的问题(这也会发出警告)在我的机器上。)

您遇到的具体问题是您丢失的案例是“如果列表为空,会发生什么,但字符串不是所有空字符串?”在这种情况下,这两个功能都会失败。事实上,你真的只关心处理列表为空时会发生什么。

至于如何处理您的输出,我建议您使用map而不是filter。从根本上说,你正在做的是改变testDatabase的元素,而不是剥离元素。为此,您必须更改checkRateRerating以输出Film而不是Bool(并且可能会将checkRateRerate更改为输出[UserRatings]而不是{{1} }})。