को देखते हुए प्रकार वर्गप्रकार वर्ग "FlexibleInstances" के विषय में समस्या
class Dictionary w where
insert :: String -> String -> w -> w
remove :: String -> w -> w
lookUp :: String -> w -> String
मैं की
instance Dictionary [(String,String)] where
insert key value dic = (key,value) : remove key dic
remove key dic = filter (\entry -> (fst entry) /= key) dic
lookUp key [] = "not found"
lookUp key ((k,v):xs) | k == key = v
| otherwise = lookUp key xs
क्योंकि
Illegal instance declaration for `Dictionary[(String, String)]'
(All instance types must be of the form (T a1 ... an)
where a1 ... an are type *variables*,
and each type variable appears at most once in the instance head.
Use -XFlexibleInstances if you want to disable this.)
In the instance declaration for `Dictionary[(String, String)]'
... जो मुझे नहीं पता नहीं लिख सकते हैं पूरी तरह समझता हूँ। इस तरह कुछ काम करता है:
newtype Dic = Dic [(String,String)]
instance Dictionary Dic where
insert key value (Dic dic) = Dic $ (key,value) : filter (\entry -> (fst entry) /= key) dic
remove key (Dic dic) = Dic $ filter (\entry -> (fst entry) /= key) dic
lookUp key (Dic []) = "not found"
lookUp key (Dic ((k,v):xs)) | k == key = v
| otherwise = lookUp key (Dic xs)
क्या कोई बेहतर तरीका है? या मुझे सुझाए गए संकलक निर्देश का उपयोग करना चाहिए?
ज़रूर, लेकिन मेरे सवाल का अधिक था कारण है कि मैं इस त्रुटि बिल्कुल मिलता है, और अगर वहाँ मानक हास्केल में मेरी प्रकार वर्ग का दृष्टांत के लिए एक कम बदसूरत तरीका है। – Landei