Это прекрасно работает:Исключение для "экземпляра (Num а) => YesNo а, где" код строки
class YesNo a where
yesNo :: a-> Bool
instance YesNo Bool where
yesNo True = True
yesNo _ = False
instance YesNo [a] where
yesNo [] = False
yesNo _ = True
instance YesNo (Maybe a) where
yesNo Nothing = False
yesNo _ = True
Но я получаю сообщение об ошибке для кода:
instance (Num a) => YesNo a where -- error is here
yesNo 0 = False
yesNo _ = True
Exception сообщение:
ghci> :l src
[1 of 1] Compiling Main (src.hs, interpreted)
src.hs:16:21:
Illegal instance declaration for `YesNo a'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are *distinct type variables*,
and each type variable appears at most once in the instance head.
Use FlexibleInstances if you want to disable this.)
In the instance declaration for `YesNo a'
Failed, modules loaded: none.
ghci>
Что я сделал неправильно?
Все функции могут быть упрощены. Для 'Bool':' yesNo = id', для '[a]': 'yesNo = нет. empty', для 'Maybe a':' yesNo = isJust'. – Shoe
@ Джеффри 'yesNo = нет. null' – augustss
@augustss Да, я все время забываю, что это 'null', а не' empty'. Я просто, похоже, испытываю глубокое отвращение к термину «null». – Shoe