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\$
1
  • \$\begingroup\$ "Each element is the sum of itself with the next element of a List", so in other words, repeat 0? :) \$\endgroup\$
    – Dan Burton
    Commented Nov 26, 2011 at 19:57

2 Answers 2

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\$
2
  • 3
    \$\begingroup\$ The fact that Haskell has an IRC bot that can refactor Haskell code blows my mind. Well played sir. \$\endgroup\$ Commented Dec 10, 2011 at 4:50
  • \$\begingroup\$ The usage of lambdabot here is amazing. I would give you 10 ups, if I could! \$\endgroup\$ Commented Apr 2, 2012 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 and acknowledge you have read our privacy policy.

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