2013-10-27 34 views
4

嗨,我很新的haskell。我試圖編寫一個代碼,允許我輸入一個不允許的座標系統列表(CS1)(即座標列表列表)和所有座標列表(CL)。該功能的目的是放棄CS1中包含CL中至少1個座標的所有座標系,最終得到來自CS1的較小座標系(CS2)子集。 (困惑?對不起)「功能監聽中的非窮盡模式」haskell

我覺得我相當接近破解堅果(雖然我真的不知道,因爲我在線索錯誤階段),但我有一個non-當我運行監聽器2時,它充斥着對監聽器的抱怨。任何人都可以看到我失蹤?謝謝你提供的所有幫助! :d

listelem0 :: (Eq a) => [a] -> a -> [a] 
listelem0 [] y = [] 
listelem0 (x:xs) y 
      | x == y = [] 
      | x /= y = [x] ++ listelem0 xs y 

listelem :: (Eq a) => [a] -> a -> [a] 
listelem (x:xs) y 
     | length (listelem0 (x:xs) y) < length (x:xs) = [] 
     | otherwise = listelem0 (x:xs) y 

listelem1 :: (Eq a) => [[a]] -> a -> [[a]] 
listelem1 [] y = [] 
listelem1 (x:xs) y = [listelem x y] ++ listelem1 xs y 

listelem2 :: (Eq a) => [[a]] -> [a] -> [[a]] 
listelem2 [] [] = [[]] 
listelem2 (x:xs) [] = (x:xs) 
listelem2 (x:xs) (y:ys) = listelem2 (listelem1 (x:xs) y) ys 

回答

4

你沒有模式這些調用:

  • listelem [] y
  • listelem2 [] (y:ys)
11

周華健是正確的,那你就錯過這些2種模式,但在這裏是如何你可以自己找到那些缺失的模式:

如果你運行ghci -Wall yourFile.hs(或ghci -fwarn-incomplete-patterns yourFile.hs),你會看到所有的不完整的模式:

[1 of 1] Compiling Main    (/tmp/ls.hs, interpreted) 

/tmp/ls.hs:2:1: 
    Warning: Pattern match(es) are non-exhaustive 
      In an equation for `listelem0': Patterns not matched: (_ : _) _ 

/tmp/ls.hs:8:1: 
    Warning: Pattern match(es) are non-exhaustive 
      In an equation for `listelem': Patterns not matched: [] _ 

/tmp/ls.hs:17:1: 
    Warning: Pattern match(es) are non-exhaustive 
      In an equation for `listelem2': Patterns not matched: [] (_ : _) 
Ok, modules loaded: Main. 

讓我們最後一個例子:In an equation for listelem2': Patterns not matched: [] (_ : _) - 這意味着僅僅這聽起來像:該模式listelem2 [] (something:somethingElse)永遠沒有匹配。

+3

FWIW,我不知道你可以這樣做... – MathematicalOrchid