我试图通过学习你是一个很好的Haskell来完成应用实例!书。
我的代码在这里:
class (Functor f) => Applicative f where
pure :: a -> f a
(<*>) :: f (a -> b) -> f a -> f b
instance Applicative Maybe where
pure = Just
Nothing <*> _ = Nothing
(Just f) <*> something = fmap f something
main = do
print $ Just (+3) <*> Just 9
print $ pure (+3) <*> Just 10
print $ pure (+3) <*> Just 9
print $ Just (++"hahah") <*> Nothing
print $ Nothing <*> Just "woot"
我收到了此错误消息:
No instance for (Show b0) arising from a use of `print'
The type variable `b0' is ambiguous
Possible fix: add a type signature that fixes these type variable(s)
Note: there are several potential instances:
instance Show Double -- Defined in `GHC.Float'
instance Show Float -- Defined in `GHC.Float'
instance (Integral a, Show a) => Show (GHC.Real.Ratio a)
-- Defined in `GHC.Real'
...plus 23 others
In the expression: print
In a stmt of a 'do' block: print $ Nothing <*> Just "woot"
In the expression:
do { print $ Just (+ 3) <*> Just 9;
print $ pure (+ 3) <*> Just 10;
print $ pure (+ 3) <*> Just 9;
print $ Just (++ "hahah") <*> Nothing;
.... }
为什么会失败,我该如何解决?
答案 0 :(得分:3)
问题出在最后print
行。基本上,您使用String -> b
应用函数<*>
。只是碰巧你使用Nothing
来包装函数,所以最后没有函数。该应用程序的结果是Maybe b
b
,其中a
是任意的,但只有那些Show
Maybe a
的实例为b
生成实例。但请记住,您有一个Show
,它不一定是print
的实例。但是Show
要求其参数是b
的实例。由于您无法证明Show
是Maybe b
的实例,因此您无法证明Show
是print
的实例,因此Maybe b
不适用于该值类型为(Nothing :: Maybe (String -> Int)) <*> ...
。
要使它消失,您必须指定您想要包含的确切类型的函数。所以结果有一些已知的类型:
print
或者你可以只提供作为print (... :: Maybe Int)
的输入传递的整个表达式的类型:
UPDATE your_tablename
SET SetType = 'CATEGORY_'+ CONVERT(NVarchar(2),SetType)