functions as applicative functors (Haskell / LYAH)

后端 未结 4 1144
既然无缘
既然无缘 2020-12-02 09:58

Chapter 11 of Learn You a Haskell introduces the following definition:

instance Applicative ((->) r) where
    pure x = (\\_ -> x)
    f <*         


        
4条回答
  •  情歌与酒
    2020-12-02 10:23

    “In the instance, substituting ((->)r) for f: r->(a->b)->(r->a)->(r->b)

    Why, that's not right. It's actually (r->(a->b)) -> (r->a) -> (r->b), and that is the same as (r->a->b) -> (r->a) -> r -> b. I.e., we map an infix and a function which returns the infix' right-hand argument, to a function which takes just the infix' LHS and returns its result. For example,

    Prelude Control.Applicative> (:) <*> (\x -> [x]) $ 2
    [2,2]
    

提交回复
热议问题