((f a) b) 和 Haskell 中的 (f a 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
在这两个例子中,((f a) b)
是否与 (f a b)
相同,因为 Haskell 中的每个函数只能接受一个参数?
是的,他们完全一样。
Haskell 将 (f a b) 转置为 ((f a) b)。这叫做柯里化。它默认对所有函数执行此操作,但可以被覆盖。
add = (+)
(add 1 2) -- becomes -- ((add 1) 2) -- upon execution.
两者都 return 3
。函数的结果就是它的值。
柯里化函数很自然。
add1 = add 1
add1 2 -- also returns 3
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
在这两个例子中,((f a) b)
是否与 (f a b)
相同,因为 Haskell 中的每个函数只能接受一个参数?
是的,他们完全一样。
Haskell 将 (f a b) 转置为 ((f a) b)。这叫做柯里化。它默认对所有函数执行此操作,但可以被覆盖。
add = (+)
(add 1 2) -- becomes -- ((add 1) 2) -- upon execution.
两者都 return 3
。函数的结果就是它的值。
柯里化函数很自然。
add1 = add 1
add1 2 -- also returns 3