Chciałem w Haskellu zdefiniować własny typ danych, Day:
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun
Następnie wiem, że będę potrzebował zamiany: Day -> Int oraz Int -> Day, więc w tym celu stworzyłem instancję:
instance Enum Day where
toEnum Mon = 1
toEnum Tue = 2
toEnum Wed = 3
toEnum Thu = 4
toEnum Fri = 5
toEnum Sat = 6
toEnum Sun = 7
fromEnum 1 = Mon
fromEnum 2 = Tue
fromEnum 3 = Wed
fromEnum 4 = Thu
fromEnum 5 = Fri
fromEnum 6 = Sat
fromEnum 7 = Sun
WinGHCi wywala mi takie błędy:
Ksiazka.hs:72:9:
Couldn't match expected typeInt' with actual type
Day'
In the pattern: Mon
In an equation fortoEnum': toEnum Mon = 1 In the instance declaration for
Enum Day'Ksiazka.hs:79:11:
No instance for (Eq Day)
arising from the literal1' Possible fix: add an instance declaration for (Eq Day) In the pattern: 1 In an equation for
fromEnum': fromEnum 1 = Mon
In the instance declaration for `Enum Day'Ksiazka.hs:79:11:
No instance for (Num Day)
arising from the literal1' Possible fix: add an instance declaration for (Num Day) In the pattern: 1 In an equation for
fromEnum': fromEnum 1 = Mon
In the instance declaration for `Enum Day'Ksiazka.hs:79:15:
Couldn't match expected typeInt' with actual type
Day'
In the expression: Mon
In an equation forfromEnum': fromEnum 1 = Mon In the instance declaration for
Enum Day
Próbowałem też wywalić definicję instancji i typ Day zdefiniować tak:
data Day = Mon | Tue | Wed | Thu | Fri | Sat | Sun deriving (Enum)
i niby się kompiluje, ale definiując następnie funkcję z metodami toEnum i fromEnum:
dayAfter :: Day -> Day
dayAfter d = fromEnum((toEnum d + 1) `mod` 7)
dostaję błąd kompilacji:
Couldn't match expected type Day' with actual type
Int'
In the return type of a call of fromEnum' In the expression: fromEnum ((toEnum d + 1)
mod7) In an equation for
dayAfter':
dayAfter d = fromEnum ((toEnum d + 1) mod
7)
Niestety, ja tych komunikatów nie rozumiem i nie wiem jak sobie poradzić.