2011-08-19 13 views
12

मुझे लागू करने वाले फ़ैक्टर और फ़ंक्चर सीखने में मदद करने के लिए मैंने सोचा कि Either टाइपक्लास Functor और Applicative के साथ लागू किया गया है। जाहिर है, मैं बस आगे बढ़ सकता हूं और कोड पढ़ सकता हूं लेकिन चीजों की बेहतर समझ प्राप्त करने के लिए मुझे चीजों को आजमाने और लागू करने के लिए और अधिक उपयोगी लगता है।डेटा को लागू करने का प्रयास कर रहे हैं। या तो

FYI करें मैं इस प्रस्तुति http://applicative-errors-scala.googlecode.com/svn/artifacts/0.6/chunk-html/index.html

वैसे भी के परिणामों की हास्केल संस्करण को लागू करने की कोशिश कर रहा हूँ, यह क्या मैं अब तक

data Validation a b = Success a | Failure b deriving (Show, Eq) 

instance Functor (Validation a) where 
    fmap f (Failure x) = Failure x 
    fmap f (Success x) = Success (f x) 

है लेकिन जब भी मैं ghci के साथ इस चलाने का प्रयास है मैं सिर्फ निम्न त्रुटि संदेश मिलता है: -

[1 of 1] Compiling Main    (t.hs, interpreted) 

t.hs:5:35: 
    Couldn't match type `b' with `a1' 
     `b' is a rigid type variable bound by 
      the type signature for 
      fmap :: (a1 -> b) -> Validation a a1 -> Validation a b 
      at t.hs:4:5 
     `a1' is a rigid type variable bound by 
      the type signature for 
      fmap :: (a1 -> b) -> Validation a a1 -> Validation a b 
      at t.hs:4:5 
    Expected type: a 
     Actual type: b 
    In the return type of a call of `f' 
    In the first argument of `Success', namely `(f x)' 
    In the expression: Success (f x) 

t.hs:5:37: 
    Couldn't match type `a' with `a1' 
     `a' is a rigid type variable bound by 
      the instance declaration at t.hs:3:30 
     `a1' is a rigid type variable bound by 
      the type signature for 
      fmap :: (a1 -> b) -> Validation a a1 -> Validation a b 
      at t.hs:4:5 
    In the first argument of `f', namely `x' 
    In the first argument of `Success', namely `(f x)' 
    In the expression: Success 

मैं सच में यकीन है कि ऐसा क्यों है नहीं कर रहा हूँ, किसी को भी मदद कर सकते हैं?

उत्तर

13

आप Success हिस्सा है, जो करने के लिए सामान्य बात है पर Functor उदाहरण काम करने के लिए कोशिश कर रहे हैं, लेकिन अपने प्रकार पैरामीटर के आदेश की वजह से यह Failure हिस्सा बजाय में प्रकार पर परिभाषित किया जा रहा है।

जब से तुम

data Validation a b = Success a | Failure b 

instance Functor (Validation a) where 
    ... 

के रूप में परिभाषित किया है इसका मतलब है कि fmap अपने क्रियान्वयन प्रकार (x -> y) -> Validation a x -> Validation a y होना चाहिए। लेकिन चूंकि दूसरा प्रकार चर Failure केस के लिए है, यह चेक टाइप नहीं करता है।

आप Success मामले के लिए प्रकार चर बजाय पिछले एक होना चाहते हैं:

data Validation b a = Success a | Failure b 
+0

आह मैं देख रहा हूँ, धन्यवाद, मैं 'मानते हुए कर रहा हूँ डेटा मान्यकरण एक ख = विफलता एक | सफलता बी प्राप्त करना (दिखाएँ, ईक) 'भी सही होगा। धन्यवाद! – djhworld

+1

@ डीजेवर्ल्ड: हाँ। टाइप वैरिएबल के नाम और कन्स्ट्रक्टर के ऑर्डर के साथ यह वही बात है। – hammar

+5

@djhworld: और अब आप यह भी समझते हैं कि, 'या तो एक बी' में, 'वाम' विफलता के मामले का प्रतिनिधित्व करता है (न सिर्फ इसलिए कि यह "सही" परिणाम नहीं है)। –

संबंधित मुद्दे

 संबंधित मुद्दे