2012-12-03 76 views
0

或者我只是失明?模式匹配失敗

非常容易簡單的函數,拋出「模式匹配失敗:get_rtg DB」

type Movie  = (Title,Regisseur,MainActors,ReleaseDate,Genre,SalesPrice) 
type Title  = String 
type Regisseur = String 
type Actor  = String 
type MainActors = [Actor] 
type ReleaseDate = Int 
data Genre  = Thriller | Fantasy | ScienceFiction | Comedy deriving (Eq,Ord,Show) 
type SalesPrice = Int 
type Database = [Movie] 


-- gets all entrys which have a Regisseur, who is in MainActors at the same time 
get_rtg :: Database -> [(Regisseur,Title,Genre)] 
get_rtg []        = [] 
ger_rtg ((ti,reg,acts,rel,gen,sal):xs) = if (isInfixOf [reg] acts) then ([(reg,ti,gen)] ++ (get_rtg xs)) else (get_rtg xs) 
+0

我看不出你如何得到該代碼模式匹配失敗。你能重新加載你的代碼,並向我們展示一個完整的ghci會話的錯誤發生? – Tarrasch

+1

你爲什麼不使用'filter'? –

+0

我知道我不知道我在說什麼,但是如果某種特殊的第三個值適合任何地方並且看起來像倒轉的T被傳遞給該函數,那麼上面的代碼不會失敗嗎? – dsign

回答

5

我想這只是一個錯字:ger_rtg在最後一行聲明瞭一個新的功能,所以get_rtg不能模式匹配現在處於非[]情況。

而且,我會用filter做這個操作:

get_rtg = filter (\(_,reg,acts,_,_,_) -> reg `elem` acts) 
+0

thx所以我只是再次失明:D –

+0

@JakobAbfalter只是不允許代碼刺激你,它會運行得更好:) –