This is short for haskell.

I tried for a long time to repeat this function in haskell, I want to express, for example:

mySum x y = x + y
mySum x y = (+) x y
mySum x = (+) x
mySum = (+) -- it Messi goal! 

My function is a little more complicated, but I really can’t do it, I looked here and there, and I know that there are some methods, for example, changing the right side and using flip. I tried and I compiled here:

zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' f x y  = map  (uncurry f) (zip x y) 

Steps:

zipWith' f x y  = map  (uncurry f) (zip x y) 
zipWith' f x y  = flip  map  (zip x y) (uncurry f)
zipWith' f x y  = flip  map  (zip x y) $ uncurry f

and then I don’t know how to proceed ...

I’m looking for an answer that can explain step by step how to achieve the “goal of Messi”, I know that there is a lot to ask, so I’ll add as soon as I can thank the effort

+6
source share
1 answer
zipWith' f x y = map (uncurry f) (zip x y)

Rewrite the application for composition and eta-reduce:

-- \y -> let g = map (uncurry f); h = zip x in (g . h) y
-- let g = map (uncurry f); h = zip x in g . h

zipWith' f x = map (uncurry f) . zip x

Rewrite the infix in the prefix:

-- g . h = (.) g h

zipWith' f x = (.) (map (uncurry f)) (zip x)

Rewrite the application for composition and eta-reduce:

-- \x -> let g = (.) (map (uncurry f)); h = zip in (g . h) x
-- let g = (.) (map (uncurry f)); h = zip in g . h

zipWith' f = (.) (map (uncurry f)) . zip

Rewrite the infix in the prefix:

-- g . h = (.) g h

zipWith' f = (.) ((.) (map (uncurry f))) zip

flip f :

-- flip f x y = f y x

zipWith' f = flip (.) zip ((.) (map (uncurry f)))

:

-- g (h (i x)) = (g . h . i) x

zipWith' f = flip (.) zip (((.) . map . uncurry) f)

eta-reduce:

-- \f -> let g = flip (.) zip; h = (.) . map . uncurry in (g . h) f
-- let g = flip (.) zip; h = (.) . map . uncurry in g . h

zipWith' = (flip (.) zip) . ((.) . map . uncurry)

:

zipWith' = flip (.) zip . (.) . map . uncurry

, :

zipWith' = (. zip) . (.) . map . uncurry

.


-> Control.Arrow. , ​​, \ f x y -> ..., , . \ (f, (x, y)) -> ...

\ (f, (x, y)) -> map (uncurry f) (zip x y)

(x, y), uncurry zip:

\ (f, (x, y)) -> map (uncurry f) (uncurry zip (x, y))
\ (f, xy) -> map (uncurry f) (uncurry zip xy)

: (uncurry uncurry zip) (f xy), ( map). *** Control.Arrow, :

(***) :: Arrow a => a b c -> a b' c' -> a (b, b') (c, c')

, thats:

(***) @(->) :: (b -> c) -> (b' -> c') -> (b, b') -> (c, c')

. !

uncurry *** uncurry zip
  :: (a -> b -> c, ([x], [y])) -> ((a, b) -> c, [(x, y)])

uncurry f f. , uncurry map:

uncurry map . (uncurry *** uncurry zip)
  :: (a -> b -> c, ([a], [b])) -> [c]

curry . : (f, xy) (x, y). curry:

curry $ uncurry map . (uncurry *** uncurry zip)
  :: (a -> b -> c) -> ([a], [b]) -> [c]

fmap f -> "" :

fmap @((->) _) :: (a -> b) -> (t -> a) -> t -> b

, , fmap curry:

fmap curry $ curry $ uncurry map . (uncurry *** uncurry zip)
  :: (a -> b -> c) -> [a] -> [b] -> [c]

! . , , :

zipWith' = untuple2 $ combineWith map apply zipped
  where
    untuple2 = fmap curry . curry
    combineWith f g h = uncurry f . (g *** h)
    apply = uncurry
    zipped = uncurry zip

, , , , . Haskell, :

zipWith' f x y = map (uncurry f) (zip x y)

:

zipWith' f = map (uncurry f) .: zip
  where (.:) = (.) . (.)
+13

All Articles