簡體   English   中英

Haskell-對數據庫中的電影進行評級或評級

[英]Haskell - Rate or Rerate a film in a Database

我的編碼是健壯的,但是由於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

怎么做:
-第一衛兵在“阿凡達”電影中覆蓋從[“ Emma”,3]到[“ Emma”,10]的比率
-第二名后衛在“雨果”電影中添加了[“ Emma”,10]

因此,在swapRate函數中,我無法同時使用兩個防護。 如果我評論第一個后衛,第二個后衛會起作用,反之亦然。 尋找一種使兩者同時起作用的方法。

為了消除警告,將空字符串""上的模式匹配項更改為_可以同時滿足checkRateRerateswapRate (在我的機器上也會發出警告)的問題。

您的特定問題是缺少的情況是“如果列表為空,但是字符串不是全部為空字符串,會發生什么?” 在這種情況下,這兩個功能都將失敗。 似乎確實是您真正在乎處理列表為空時發生的情況。

至於如何處理輸出,我建議您使用map而不是filter 從根本上講,您正在做的是更改testDatabase元素,而不是剝離元素。 為此,您必須將checkRateRerating更改為輸出Film而不是Bool (也許將checkRateRerate更改為輸出[UserRatings]而不是Bool )。

暫無
暫無

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

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