Map. folding compound - Haskell

So, let right to the point.

:t (map.foldr)
(map.foldr) :: (a1 -> a -> a) -> [a] -> [[a1] -> a]

What is [[a1] → a]? I am really trying to understand this composition, so I did this:

-- map.foldr

    map.foldr :: (a1 -> a -> a) -> [a] -> [[a1] -> a]

    map :: (a1 -> b1) -> [a1] -> [b1]
    (.) :: (y -> w) -> (x -> y) -> x -> w
    foldr :: (a -> b -> b) -> b -> [a] -> b

    y = (a1 -> b1)      w = ([a1] -> [b1])
    x = (a -> b -> b)   y = (b -> [a] -> b)

    y = (a1 -> b1)  
    y = (b -> [a] -> b)
_________________________

What happens at this point? Thank!

+5
source share
3 answers

To answer this question, it’s good to remember what foldrand do map.

The more complex of the two is foldrwhich is of type

--              list to be folded
--                              v
foldr :: (a -> b -> b) -> b -> [a] -> b
--             ^          ^
--folding function        terminal value

A folded list really is a chain of conses (:)and an empty list of terminals:

1 : 2 : 3 : []

The action foldris the replacement of designers :and []folding function and the terminal value, respectively:

foldr (+) 0 (1 : 2 : 3 : []) == 1 + 2 + 3 + 0

map . - :

map :: (a -> b) -> [a] -> [b]
--        ^         ^
-- function         list

, , :

map :: (a -> b) -> ( [a] -> [b] )
--        ^              ^
-- function              function on lists

, map . foldr? , - ,

(map . foldr) f == map (foldr f)

foldr, f :: a -> b -> b, :

foldr f :: b -> [a] -> b
--         ^     ^
--terminal val   list to be folded

map, , :

map (foldr f) :: [b] -> [[a] -> b]
--                ^          ^
--list of terminal vals      functions that fold lists

, . , - .


, (+),

(+) :: Num a => a -> a -> a

,

(map . foldr) (+) :: Num a => [a] -> [[a] -> a]
--                             ^          ^
--         list of terminal vals          functions that fold lists

[0, 1, 2], :

(map . foldr) (+) [0,1,2] :: Num a => [[a] -> a]

map ($x), . , [3,4,5]. :

> map ($[3,4,5]) ((map.foldr) (+) [0,1,2])
[12, 13, 14]

[3,4,5] , (+) , :

3 + 4 + 5 + 0 == 12
3 + 4 + 5 + 1 == 13
3 + 4 + 5 + 2 == 14

0, : 3 + 4 + 5 == 12. 1, , (13), 2, (14).

+14

, , y :

y = (a1 -> b1) = (b ->  [a] -> b)
               = (b -> ([a] -> b))

, :

a1 = b
b1 = [a] -> b

, :

x -> w

:

x = a -> b -> b
w = [a1] -> [b1] = [b] -> [[a] -> b]

, :

(x -> w) = ((a -> b -> b) -> ([b] -> [[a] -> b]))
         =  (a -> b -> b) ->  [b] -> [[a] -> b]

:

(a1 -> a -> a) -> [a] -> [[a1] -> a]
+2
map.foldr :: (a1 -> a -> a) -> [a] -> [[a1] -> a]

map :: (a1 -> b1) -> [a1] -> [b1]
(.) :: (y -> w) -> (x -> y) -> x -> w
foldr :: (a -> b -> b) -> b -> [a] -> b

-- if you substitute: x = (a -> b -> b)   y = (b -> [a] -> b)
-- then you get for map :: (b -> ([a] -> b)) -> [b] -> [[a] -> b]
-- so if composition operator applied:
map . foldr :: (a -> b -> b) -> [b] -> [[a] -> b]
+1
source

All Articles