2017-02-26 80 views
-2

程序是假設在大學哈斯克爾無法比擬型

data Etudiant = Etudiant CodePermanent Nom Prenom CodeProgramme deriving Show 
data Inscription = Inscription CodePermanent Sigle NoGroupe CodeSession Date Date Note deriving (Show, Eq) 

getCodePermanent :: Etudiant -> CodePermanent 
getCodePermanent (Etudiant codePermanent _ _ _) = codePermanent 

listeInscription :: Inscription -> Bool 
listeInscription (Inscription _ _ _ codeSession _ _ _) = codeSession == 32003 

filtreInscription1 :: [Inscription] -> [Inscription] 
filtreInscription1 linscription = filter listeInscription linscription 

getNoGroupe2 :: Inscription -> NoGroupe 
getNoGroupe2 (Inscription _ _ noGroupe _ _ _ _) = noGroupe 

numGroupesCoursEtu :: [Inscription] -> Etudiant -> [NoGroupe] 
numGroupesCoursEtu listeInscr etu = map getNoGroupe2(filter (\x -> getCodePermanent(x) == getCodePermanent(etu)) listeInscr) 

這裏fonctions的目標是從列表[題記]匹配的會話列表[NoGroupe]解壓處理學生代碼32003和學生的ID:CodePermanent

我的最後一行代碼給出了類型的錯誤...你們可以看到這個問題嗎?

錯誤:

TP1.hs:115:54: error: 
• Couldn't match type ‘Etudiant’ with ‘Inscription’ 
    Expected type: [Inscription] 
    Actual type: [Etudiant] 
• In the second argument of ‘map’, namely 
    ‘(filter 
     (\ x -> getCodePermanent (x) == getCodePermanent (etu)) 
     listeInscr)’ 
    In the expression: 
    map 
     getNoGroupe2 
     (filter 
     (\ x -> getCodePermanent (x) == getCodePermanent (etu)) listeInscr) 
    In an equation for ‘numGroupesCoursEtu’: 
     numGroupesCoursEtu listeInscr etu 
     = map 
      getNoGroupe2 
      (filter 
       (\ x -> getCodePermanent (x) == getCodePermanent (etu)) listeInscr) 

TP1.hs:115:114: error: 
• Couldn't match type ‘Inscription’ with ‘Etudiant’ 
    Expected type: [Etudiant] 
    Actual type: [Inscription] 
• In the second argument of ‘filter’, namely ‘listeInscr’ 
    In the second argument of ‘map’, namely 
    ‘(filter 
     (\ x -> getCodePermanent (x) == getCodePermanent (etu)) 
     listeInscr)’ 
    In the expression: 
    map 
     getNoGroupe2 
     (filter 
     (\ x -> getCodePermanent (x) == getCodePermanent (etu)) listeInscr) 

線115碼numGroupesCoursEtu的最後一行.....

+0

您是否嘗試仔細閱讀錯誤消息?如果是這樣,你有什麼困惑嗎? – jberryman

+0

我不明白如何讓它們匹配... –

+0

傳遞'銘文'時應該怎樣做'getCodePermanent'?你的代碼說什麼?當你調用'getCodePermanent(etu)'時,你將'getCodePermanent'傳遞給'Inscription'的__list__,但它期望一個'Etudiant'。 – crockeea

回答

0

getCodePermanent :: Etudiant - > CodePermanent

注意getCodePermanent需要Etudiant作爲一個參數。

(\x -> getCodePermanent(x) == getCodePermanent(etu)) 

想必您要檢查是否有Inscription具有相同的CodePermanent作爲給定Etudiant。但正如我剛纔所說,getCodePermanentEtudiant作爲參數。您需要編寫另一個函數以從Inscription獲取CodePermanent

+0

謝謝!我添加這兩行:getCodePermanent2 ::銘文 - > CodePermanent getCodePermanent2(銘文codePermanent _ _ _ _ _ _)= codePermanent 現在它的工作! –

+0

@JiaMingWang就是這個想法。雖然,我建議找一個更好的名字。 –

+0

@JiaMingWang請記得在您能夠接受我的答案時 –