![](/img/trans.png)
[英]Haskell: Is there an operator like this: (<$$>) :: Functor f => f a -> (a -> b) -> f b
[英]Is ((f a) b) the same as (f a b) in Haskell?
map2_Maybe :: (a -> b -> c) -> Maybe a -> Maybe b -> Maybe c
map2_Maybe f Nothing _ = Nothing
map2_Maybe f (Just a) Nothing = Nothing
map2_Maybe f (Just a) (Just b) = Just ((f a) b)
-- Or: map2_Maybe f (Just a) mb = fmap (f a) mb
map2_Either :: (a -> b -> c) -> Either e a -> Either e b -> Either e c
map2_Either f (Left e) _ = Left e
map2_Either f (Right a) (Left e) = Left e
map2_Either f (Right a) (Right b) = Right (f a b)
-- Or: map2_Either f (Right a) eb = fmap (f a) eb
在这两个示例中,由于Haskell中的每个函数只能接受一个参数, ((fa) b)
是否与(fab)
相同?
是的,它们是完全一样的。
Haskell将(fab)转置为((fa)b)。 叫做咖喱。 默认情况下,它对所有功能都执行此操作,但是可以覆盖它。
add = (+)
(add 1 2) -- becomes -- ((add 1) 2) -- upon execution.
两者都返回3
。 函数的结果就是它的值。
咖喱函数是自然的。
add1 = add 1
add1 2 -- also returns 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.