HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Each element is the sum of itself with the next element of a List: now do this point-free in Haskell

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
itselfthistheeachpointwithfreenextnowelement

Problem

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.

Solution

Pointfree, eh? Let's ask lambdabot.

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


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

 @pl \x -> map (\(y, z) -> y + z) (zip x (tail x))
 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.

Code Snippets

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

Context

StackExchange Code Review Q#6299, answer score: 7

Revisions (0)

No revisions yet.