2017-02-26 84 views
1

我需要一些幫助。在代碼的最後一行我總是得到一個類型匹配錯誤,但我不明白爲什麼'因爲它似乎是好的... isGroupInscr(x)(y)返回true或false,我將它與真正的沒有不匹配。 .. 你能給我一些建議嗎? :)Haskell Lambda表達式和匹配類型

isGroupInscr :: GroupeCours -> Inscription -> Bool 
isGroupInscr sigleGroup sigleInscr = getSigle(sigleGroup) /= getSigle2(sigleInscr) 

filtreGroupInscr :: [GroupeCours] -> [Inscription] -> [GroupeCours] 
filtreGroupInscr listGroupe listInscr = filter (\x y -> isGroupInscr(x)(y) == True) listGroupe 

錯誤:

• Couldn't match expected type ‘Bool’ 
       with actual type ‘Inscription -> Bool’ 
• The lambda expression ‘\ x y -> isGroupInscr (x) (y) == True’ 
    has two arguments, 
    but its type ‘GroupeCours -> Bool’ has only one 
    In the first argument of ‘filter’, namely 
    ‘(\ x y -> isGroupInscr (x) (y) == True)’ 
    In the expression: 
    filter (\ x y -> isGroupInscr (x) (y) == True) listGroupe 

感謝

+0

你不需要在這裏lambda表達式:簡單'isGroupInscr'足以在'filter'第一個參數。 –

+0

你可以編輯你的問題,包括整個程序? (或者在[一些pastebin](https://hastebin.com)中提供一個到源代碼的鏈接?) –

+0

沒有類型定義和規範就無法回答這個問題 - 你想做什麼?你根本沒有使用'listInscr'參數,所以這看起來相當不對。可能你需要對這兩個列表進行壓縮,然後進行過濾,或者使用「任意」或自定義遞歸來搜索課程中是否出現錯誤。 – chi

回答

0

您需要申請一部分

isGroupInscr :: GroupeCours -> Inscription -> Bool 

獲得已鍵入

Inscription -> Bool 
功能

能夠與filter :: (a -> Bool) -> [a] -> [a]


從類型的函數猜測使用,我想你想要的是

isGroupInscr :: GroupeCours -> Inscription -> Bool 
isGroupInscr sigleGroup sigleInscr = getSigle sigleGroup /= getSigle2 sigleInscr 

filtreGroupInscr :: [GroupeCours] -> [Inscription] -> [GroupeCours] 
filtreGroupInscr (g:gs) (l:ls) = if isGroupInscr g l 
           then g : filtreGroupInscr gs ls 
           else [] 
filtreGroupInscr _  _  = [] 

其他可能的猜測

filtreGroupInscr listGroupe listInscr 
    = [g | g <- listGroupe , l <- listInscr, isGroupInscr g l] 

而且,注意到你不必使用()來調用函數,除非它確實需要。 Haskell將空間用作函數應用程序。


側面說明:哈斯克爾undefined魔術,我能夠加載不完整的代碼GHCI做類型檢查。

代碼test3.hs

data GroupeCours = GroupeCours 
data Inscription = Inscription 
data Single = Single deriving Eq 

getSigle :: GroupeCours -> Single 
getSigle = undefined 

getSigle2 :: Inscription -> Single 
getSigle2 = undefined 

isGroupInscr :: GroupeCours -> Inscription -> Bool 
isGroupInscr sigleGroup sigleInscr = getSigle sigleGroup /= getSigle2 sigleInscr 

filtreGroupInscr :: [GroupeCours] -> [Inscription] -> [GroupeCours] 
filtreGroupInscr (g:gs) (l:ls) = if isGroupInscr g l 
           then g : filtreGroupInscr gs ls 
           else [] 
filtreGroupInscr _  _  = [] 

filtreGroupInscr' :: [GroupeCours] -> [Inscription] -> [GroupeCours] 
filtreGroupInscr' listGroupe listInscr 
    = [g | g <- listGroupe , l <- listInscr, isGroupInscr g l] 

GHCI行動

$ ghci 
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help 
Loaded GHCi configuration from /data/works/dotfiles/ghci 
Prelude> :l test3.hs 
[1 of 1] Compiling Main    (test3.hs, interpreted) 
Ok, modules loaded: Main. 
*Main> :t isGroupInscr 
isGroupInscr :: GroupeCours -> Inscription -> Bool 
*Main> :t filtreGroupInscr 
filtreGroupInscr :: [GroupeCours] -> [Inscription] -> [GroupeCours] 
*Main> :t filtreGroupInscr' 
filtreGroupInscr' 
    :: [GroupeCours] -> [Inscription] -> [GroupeCours]