Haskell - creating a function definition for the function `all` using foldr

后端 未结 1 669
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 12:45

I\'m trying to create a function definition for the function all using foldr. p is the predicate. I know this can be done:

         


        
1条回答
  •  抹茶落季
    2021-01-29 13:04

    We have

    all p = and 
             . foldr (\x xs -> p x :  xs) []    
          = foldr                 (&&)   True   -- {y : ys} -> y && {ys}      2-3
             . foldr (\x xs -> p x :  xs) []    -- {x , xs} -> p x : {xs}   1-2
          =    foldr (\x xs -> p x && xs) True  -- {x , xs} -> p x && {xs}  1---3
    

    because folding replaces each constructor with the specified combination operation (aka reducer), and replacing a cons of an element with a cons of a modified element, and then replacing that cons with (&&), is just replacing a cons of an element with the (&&) of a modified element right away:

        a  : (  b  : (  c  : (  d  : ( ... ))))   _OR_   []      --   |       |   1
                                                                 --   |       |
      p a  : (p b  : (p c  : (p d  : ( ... ))))   _OR_   []      --   ↓   |   |   2
                                                                 --       |   |
      p a && (p b && (p c && (p d && ( ... ))))   _OR_  True     --       ↓   ↓   3
    

    In other words, folds compose by fusing their reducer functions, and reducer functions fuse by replacing the {constructors they use} with the next fold's reducer in the chain of folds, so that their corresponding transducers compose (as in Clojure's transducers); thus,

     = foldr              (reducingWith (&&)) True
         . foldr ((mapping p)    (:))           []
     = foldr ((mapping p) (reducingWith (&&))) True
     = foldr ((mapping p . reducingWith) (&&) ) True
       -- first map p, then reduce with (&&)
    

    for the appropriate definitions of reducingWith and mapping:

    reducingWith cons x xs = cons x xs
    mapping f cons x xs = cons (f x) xs
    filtering p cons x xs | p x = cons x xs
                          | otherwise = xs
    concatting t cons x xs = foldr cons xs (t x)
    

    0 讨论(0)
提交回复
热议问题