Reputation: 49816
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?
Upvotes: 5
Views: 305
Reputation: 10541
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)]
Upvotes: 3
Reputation: 139830
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
Upvotes: 11
Reputation: 183873
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.
Upvotes: 6