Domanda

A function which zips a list onto itself can be defined as:

let adjacent1 l = zip l $ tail l

This works, but I'd like to define it in pointfree style. To do this, I define a function dollarize:

let dollarize f1 f2 x = f1 x $ f2 x
let adjacent1 = dollarize zip tail

This works, but obviously I'd rather not define my own higher-order functions. Is there a way to find the standard equivalent of dollarize, assuming it exists? And if not, where are the functions of this sort which exist to combine functions?

È stato utile?

Soluzione

The pointfree tool can do this for you automagically.

$ pointfree "\l -> zip l (tail l)"
ap zip tail
$ pointfree "\f1 f2 x -> f1 x $ f2 x"
ap

Altri suggerimenti

How about using the Applicative instance of (->) a?

Prelude Control.Applicative> :t zip <*> tail
zip  <*> tail :: [a] -> [(a, a)]
Prelude Control.Applicative> zip <*> tail $ [1 .. 4]
[(1,2),(2,3),(3,4)]

short and sweet.

Accoring to @Daniel Fischer's answer. Also your can use monad instance of (->) a:

Prelude Control.Monad.Instances> let adjacent1 = tail >>= flip zip
Prelude Control.Monad.Instances> adjacent1 [1..4]
[(1,2),(2,3),(3,4)]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top