Use GHC 7.10
The first version of GHC that contained this stuff was released on March 27, 2015.
It's the latest version, and Prelude got some new additions that are useful for golfing:
The (<$>)
and (<*>)
operators
These useful operators from Data.Applicative
made it in! <$>
is just fmap
, so you can replace map f x
and fmap f x
with f<$>x
everywhere and win back bytes. Also, <*>
is useful in the Applicative
instance for lists:
Prelude> (,)<$>[1..2]<*>"abcd"
[(1,'a'),(1,'b'),(1,'c'),(1,'d'),(2,'a'),(2,'b'),(2,'c'),(2,'d')]
The (<$)
operator
x<$a
is equivalent to fmap (const x) a
; i.e. replace every element in a container by x
.
This is often a nice alternative to replicate
: 4<$[1..n]
is shorter than replicate n 4
.
The Foldable/Traversable Proposal
The following functions got lifted from working on lists [a]
to general Foldable
types t a
:
fold*, null, length, elem, maximum, minimum, sum, product
and, or, any, all, concat, concatMap
This means they now also work on Maybe a
, where they behave just like "lists with at most one element". For example, null Nothing == True
, or sum (Just 3) == 3
. Similarly, length
returns 0 for Nothing
and 1 for Just
values. Instead of writing x==Just y
you can write elem y x
.
You can also apply them on tuples, which works as if you'd called \(a, b) -> [b]
first. It's almost completely useless, but or :: (a, Bool) -> Bool
is one character shorter than snd
, and elem b
is shorter than (==b).snd
.
The Monoid functions mempty
and mappend
Not often a life-saver, but if you can infer the type, mempty
is one byte shorter than Nothing
, so there's that.