简体   繁体   中英

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

In these two examples, Is ((fa) b) the same as (fab) since every function in Haskell can only take one argument?

是的,它们是完全一样的。

Haskell transposes (fab) into ((fa) b). It's called currying. It does that to all functions by default but can be overridden.

 add = (+)
(add 1 2) -- becomes --  ((add 1) 2) -- upon execution.

Both return 3 . The result of a function is its value.

Curried functions are natural.

add1 = add 1
add1 2 -- also returns 3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM