8
\$\begingroup\$

Here it is the best I can do (I am a noob using Haskell):

map (\(y, z) -> y + z) (zip x (tail x))

I'm looking for a point-free solution.

\$\endgroup\$
  • \$\begingroup\$ "Each element is the sum of itself with the next element of a List", so in other words, repeat 0? :) \$\endgroup\$ – Dan Burton Nov 26 '11 at 19:57
7
\$\begingroup\$

Pointfree, eh? Let's ask lambdabot.

<DanBurton> @pl map (\(y, z) -> y + z) (zip x (tail x))
<lambdabot> zipWith (+) x (tail x)

Assuming that x is simply the input to this "function"

<DanBurton> @pl \x -> map (\(y, z) -> y + z) (zip x (tail x))
<lambdabot> map (uncurry (+)) . ap zip tail

Personally I'd go with the former; I'm not a fan of Lambdabot's gratuitous use of uncurry and ap.

\$\endgroup\$
  • 3
    \$\begingroup\$ The fact that Haskell has an IRC bot that can refactor Haskell code blows my mind. Well played sir. \$\endgroup\$ – Daniel Huckstep Dec 10 '11 at 4:50
  • \$\begingroup\$ The usage of lambdabot here is amazing. I would give you 10 ups, if I could! \$\endgroup\$ – Hugo Sereno Ferreira Apr 2 '12 at 12:07
2
\$\begingroup\$

You could use &&& from Control.Arrow (along with the zipWith trick from Dan's solution):

foo = uncurry (zipWith (+)) . (tail &&& id)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.