2017-02-26 40 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请记得在您能够接受我的答案时 –