2017-02-25 16 views
0

試圖實現與Maybe相同的應用程序。在compliation 得到噸的錯誤,最喜歡定製應用程序可能

Couldn't match expected type ‘FixMePls b’ 
      with actual type ‘Maybe a1’ 

的代碼是我做錯了什麼,歡迎如下

data FixMePls a = FixMe | Pls a deriving (Eq, Show) 

instance Monoid a => Monoid (FixMePls a) where 
    mempty = Nothing 
    mappend m Nothing = m 
    mappend Nothing m = m 
    mappend (Pls a) (Pls a') = Pls (mappend a a') 

instance Applicative FixMePls where 
    pure = Pls 
    Nothing <*> _ = Nothing 
    _ <*> Nothing = Nothing 
    Pls f <*> Pls a = Pls f a 


main :: IO() 
main = do 
    putStrLn("Weee!!!1!") 

任何提示。我懷疑它是數據類型聲明,但不知道如何解決它。

+2

它告訴你,這是期待'FixMePls B'(即或者一個'FixMe'或一個'Pls'),但它有一個'Maybe a'(即'Just'或'Nothing')。 –

回答

6
data FixMePls a = FixMe | Pls a deriving (Eq, Show) 

是確定什麼錯一個

instance Monoid a => Monoid (FixMePls a) where 
    mempty = Nothing 
    mappend m Nothing = m 
    mappend Nothing m = m 
    mappend (Pls a) (Pls a') = Pls (mappend a a') 

這裏你開始混合MaybeFixmePls - NothingMaybeFixme構造函數是一個你需要的。

instance Monoid a => Monoid (FixMePls a) where 
    mempty = FixMe 
    mappend m FixMe = m 
    mappend FixMe m = m 
    mappend (Pls a) (Pls a') = Pls (mappend a a') 

我ommiting的應用性的實例 - 因爲它應該是非常相似的,如果你有問題 - 我發現了一個parenthization錯誤;)