# Tips for golfing in Haskell

Animesh 'the CODER' 09/14/2017. 30 answers, 5.564 views

What general tips do you have for golfing in Haskell? I am looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to Haskell. Please post only one tip per answer.

If you are new to golfing in Haskell please have a look at the Guide to Golfing Rules in Haskell.

1 Animesh 'the CODER' 01/24/2014
Seeing the number of answers till now, I am in doubt about whether Haskell is even a good language for code golfing or not?
6 unclemeat 02/12/2014
Why only one tip per answer? Also, every language is a good language for golfing. Just don't always expect to win.
3 MasterMastic 06/01/2014
@unclemeat This way people could upvote the good ones to the top without upvoting the bad ones only because they were written by the same guy in the same answer.
unclemeat 06/12/2014
@MasterMastic Reasonable. Thanks, I'll keep that in mind.
1 J Atkin 02/20/2016
Special request, String compression.

shiona 08/23/2015.

# Define infix operators instead of binary functions

This saves usually one or two spaces per definition or call.

0!(y:_)=y
x!(y:z)=(x-1)!z

vs.

f 0(y:_)=y
f x(y:z)=f(x-1)z

The available symbols for 1-byte operators are !, #, %, &, and ?. All other ASCII punctuation is either already defined as an operator by the Prelude (such as $) or has a special meaning in Haskell's syntax (such as @). If you need more than five operators, you could use combinations of the above, such as !#, or certain Unicode punctuation characters, such as these (all 2 bytes in UTF-8): ¡ ¢ £ ¤ ¥ ¦ § ¨ © ¬ ® ¯ ° ± ´ ¶ · ¸ ¿ × ÷ ##### 1 comments 4 Zgarb 10/15/2015 Note: this can also be used for functions with three or more arguments. (x!y)z=x+y*z and (x#y)z u=x*z+y*u both work as expected. shiona 06/24/2015. Use pointless (or -free) notation where appropriate Often a function with one or two parameters can be written point free. So a lookup for a list of tuples whose elements are swapped is naïvely written as: revlookup :: Eq b => b -> [(a, b)] -> Maybe a revlookup e l=lookup e(map swap l) (the type is there just to help you understand what it's doing.) for our purposes this is much better: revlookup=(.map swap).lookup Flonk 08/21/2014. # interact :: (String → String) → IO () People often forget that this function exists - it grabs all of stdin and applies it to a (pure) function. I often see main-code along the lines of main=getContents>>=print.foo while main=interact$show.foo

is quite a bit shorter. It is in the Prelude so no need for imports!

Flonk 08/17/2014.

Use 1<2 instead of True and 1>2 instead of False.

g x|x<10=10|True=x
f x|x<10=10|1<2=x
2 Peter Taylor 04/07/2014
This isn't really specific to Haskell: it's applicable to just about every language which has a Boolean type as opposed to truthy and falsy values of other types.
MasterMastic 06/01/2014
Can anyone explain this?
this isn't a good example, i would just golf this as f=max 10.
@MasterMastic this is just writing if(true) in other languages. in the prelude, otherwise is actually the boolean value True.
1 flawr 06/11/2016
@PeterTaylor I think this is still valuable for new haskellians (like me) as I first learned to use otherwise.

Lynn 06/16/2016.

# Use GHC 7.10

Jan Dvorak noted that you can only use language versions released before the challenge was made. The first version of GHC that contained this stuff was released on March 27, 2015.

## 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. ##### 4 comments 5 ankh-morpork 07/10/2015 +1 It's great to hear about '<$> and <*> making it into Prelude! That should be usefull even when It's not code golf (applicative is such a long word).
John Dvorak 07/11/2015
Funny there is [1..2] in there. that's just [1,2]
Angs 11/07/2016
In the same version we also got <* from Applicative, which for lists is xs <* ys == concatMap (replicate (length ys)) xs. This is different from xs >> ys or xs *> ys which is concat (replicate (length ys)) xs. pure which is a shorter return came at this point too.

bazzargh 10/24/2016.

Use guards not conditionals:

f a=if a>0 then 3 else 7
g a|a>0=3|True=7

Use semicolons not indents

f a=do
this
that
g a=do this;that

Use boolean expressions for boolean functions

f a=if zzz then True else f yyy
g a=zzz||f yyy

(SO is being a pain about letting me post these separately)

2 John Dvorak 03/21/2014
also, use multiple guards instead of && when inside a list comprehension.
bazzargh 03/21/2014
Good one Jan - you should make that into an answer, I'll vote for it
1 John Dvorak 02/22/2016
The first example can be further shortened by True => 1>0
1 Cyoce 04/02/2016
in the first example, I assume you mean f a=if a>0 then 3 else 7
Christian Irwan 10/23/2016
Guard even works if there's no argument in it.

Lynn 07/10/2015.

A quick review:

Lynn 08/23/2015.

# Know your Prelude

Fire up GHCi and scroll through the Prelude documentation. Whenever you cross a function that has a short name, it can pay off to look for some cases where it might be useful.

For example, suppose you wish to transform a string s = "abc\ndef\nghi" into one that's space-separated, "abc def ghi". The obvious way is:

unwords$lines s But you can do better if you abuse max, and the fact that \n < space < printable ASCII: max ' '<$>s

Another example is lex :: String -> [(String, String)], which does something quite mysterious:

Prelude> lex "   some string of Haskell tokens  123  "
[("some"," string of Haskell tokens  123  ")]

Try fst=<<lex s to get the first token from a string, skipping whitespace. Here is a clever solution by henkma that uses lex.show on Rational values.

nyuszika7h 08/17/2014.

Use words instead of a long list of strings. This isn't really specific to Haskell, other languages have similar tricks too.

["foo","bar"]
words"foo bar"  -- 1 byte longer
["foo","bar","baz"]
words"foo bar baz"  -- 1 byte shorter
["foo","bar","baz","qux"]
words"foo bar baz qux"    -- 3 bytes shorter

Lynn 07/10/2015.

# Use list comprehensions (in clever ways)

Everyone knows they're useful syntax, often shorter than map + a lambda:

Prelude> [[1..x]>>show x|x<-[1..9]]
["1","22","333","4444","55555","666666","7777777","88888888","999999999"]

Or filter (and optionally a map at the same time):

Prelude> [show x|x<-[1..60],mod 60x<1]
["1","2","3","4","5","6","10","12","15","20","30","60"]

But there are some weirder uses that come in handy now and then. For one, a list comprehension doesn't need to contain any <- arrows at all:

Prelude> [1|False]
[]
Prelude> [1|True]
[1]

Which means instead of if p then[x]else[], you can write [x|p]. Also, to count the number of elements of a list satisfying a condition, intuitively you would write:

is longer than

m(Just c)=c;f x=m$… is longer than f x|Just c<-…=c In fact, they’re shorter even when binding a plain old value instead of deconstructing: see xnor’s tip. ##### 2 comments proud haskeller 08/26/2015 Well, the lambda one doesn't need the dollar sign, and it seems this change makes the lengths of this and the next snippet the same 1 Lynn 08/26/2015 I'm assuming e isn't actually one token but a longer expression that needs $ before it, which is usually the case.

Flonk 04/13/2017.

# Lambda parsing rules

A lambda-expression doesn't actually need parentheses around it - it just rather greedily grabs everything so the whole thing still parses, e.g. until

• a closing paren - (foo$\x -> succ x) • an in - let a = \x -> succ x in a 4 • the end of the line - main = getContents>>= \x -> head$ words x
• etc..

is encountered, and there are some weird edge-cases where this can save you a byte or two. I believe \ can also be used to define operators, so when exploiting this you will need a space when writing a lambda directly after an operator (like in the third example).

Here is an example of where using a lambda was the shortest thing I could figure out. The code basically looks like:

a%f=...
f t=sortBy(% \c->...)['A'..'Z']

Zgarb 04/13/2017.

## Try rearranging function definitions and/or arguments

You can sometimes save a couple of bytes by changing the order of pattern-matching cases in a function definition. These savings are cheap, but easy to overlook.

As an example, consider the following earlier version of (a part of) this answer:

(g?x)[]=x
(g?x)(a:b)=g(g?x$b)a This is a recursive definition of ?, with the base case being the empty list. Since [] is not a useful value, we should swap the definitions and replace it with the wildcard _ or a dummy argument y, saving a byte: (g?x)(a:b)=g(g?x$b)a
(g?x)y=x

From the same answer, consider this definition:

f#[]=[]
f#(a:b)=f a:f#b

The empty list occurs in the return value, so we can save two bytes by swapping the cases:

f#(a:b)=f a:f#b
f#x=x

Also, the order of function arguments can sometimes make a difference by allowing you to remove unnecessary whitespace. Consider an earlier version of this answer:

h p q a|a>z=0:h p(q+2)(a-1%q)|1<2=1:h(p+2)q(a+1%p)

There's an annoying piece of whitespace between h and p in the first branch. We can get rid of it by defining h a p q instead of h p q a:

h a p q|a>z=0:h(a-1%q)p(q+2)|1<2=1:h(a+1%p)(p+2)q

Zgarb 04/13/2017.

## Replace let by lambda

This can usually shorten a lone auxiliary definition that can't be bound with a guard or defined globally for some reason. For example, replace

let c=foo a in bar

by the 3 bytes shorter

(\c->bar)$foo a For multiple auxiliary definitions, the gain is probably smaller, depending on the number of definitions. let{c=foo a;n=bar a}in baz (\c n->baz)(foo a)$bar a

let{c=foo a;n=bar a;m=baz a}in qux
(\c n m->qux)(foo a)(bar a)$baz a let{c=foo a;n=bar a;m=baz a;l=qux a}in quux (\c n m l->quux)(foo a)(bar a)(baz a)$qux a

If some of the definitions refer to the others, it is even harder to save bytes this way:

let{c=foo a;n=bar c}in baz
(\c->(\n->baz)$bar c)$foo a

The main caveat with this is that let allows you to define polymorphic variables, but lambdas do not, as noted by @ChristianSievers. For example,

let f=length in(f["True"],f[True])

results in (1,1), but

(\f->(f["True"],f[True]))length

gives a type error.

Christian Sievers 01/17/2017
It rarely matters, but "semantically equivalent" promises a bit too much. We have polymorpic let, so we can do let f=id in (f 0,f True). If we try to rewrite this with lambda it doesn't type check.
Zgarb 01/18/2017
@ChristianSievers That's true, thanks for the note. I edited it in.

xnor 07/20/2016.

## Don't waste the "otherwise" guard

A final guard that's a catch-all True (shorter as 1>0) can be used to bind a variable. Compare:

... |1>0=1/(x+y)
... |z<-x+y=1/z

... |1>0=sum l-sum m
... |s<-sum=s l-s m

Since the guard is mandatory and is otherwise wasted, little is needed to make this worth it. It's enough to save a pair of parens or bind a length-3 expression that's used twice. Sometimes you can negate guards to make the final case be the expression that best uses a binding.

nimi 08/08/2016.

## Use , instead of && in guards

Multiple conditions in a guard that all have to hold can be combined with , instead of &&.

f a b | a/=5 && b/=7 = ...
f a b | a/=5 ,  b/=7 = ...
2 BlackCap 12/16/2016
It doesn't have to be conditions either, you can do things like this: f xs m | [x] <- xs, Just y <- m, x > 3 = y

xnor 08/09/2016.

## Match a constant value

A list comprehension can pattern match on a constant.

[0|0<-l]

This extracts the 0's of a list l, i.e. makes a list of as many 0's as are in l.

[1|[]<-f<$>l]  This makes a list of as many 1's as there are elements of l that f takes to the empty list (using <$> as infix map). Apply sum to count these elements.

Compare:

[1|[]<-f<$>l] [1|x<-l,f x==[]] [x|(0,x)<-l] A constant can be used as part of a pattern match. This extracts the second entries of all tuples whose first entry is 0. Note that all of these require an actual constant literal, not a the value of a variable. For example, let x=1 in [1|x<-[1,2,3]] will output [1,1,1], not [1], because the outer x binding is overwritten. Zgarb 04/13/2017. ## Working with the minus sign The minus sign - is an annoying exception to many syntax rules. This tip lists some short ways of expressing negation and subtraction in Haskell. Please let me know if I've missed something. ### Negation • To negate an expression e, just do -e. For example, -length[1,2] gives -2. • If e is even moderately complex, you will need parentheses around e, but you can usually save a byte by moving them around: -length(take 3 x) is shorter than -(length$take 3 x).
• If e is preceded by = or an infix operator of fixity less than 6, you need a space: f= -2 defines f and k< -2 tests if k is less than -2. If the fixity is 6 or greater, you need parens: 2^^(-2) gives 0.25. You can usually rearrange stuff to get rid of these: for example, do -k>2 instead of k< -2.
• Similarly, if ! is an operator, then -a!b is parsed as (-a)!b if the fixity of ! is at most 6 (so -1<1 gives True), and -(a!b) otherwise (so -[1,2]!!0 gives -1). The default fixity of user-defined operators and backticked functions is 9, so they follow the second rule.
• To turn negation into a function (to use with map etc), use the section (0-).

### Subtraction

• To get a function that subtracts k, use the section (-k+), which adds -k. k can even be a pretty complex expression: (-2*length x+) works as expected.
• To subtract 1, use pred instead, unless it would require a space on both sides. This is rare and usually happens with until or a user-defined function, since map pred x can be replaced by pred<$>x and iterate pred x by [x,x-1..]. And if you have f pred x somewhere, you should probably define f as an infix function anyway. See this tip. Lynn 02/19/2017. # Arguments can be shorter than definitions I just got outgolfed in a very curious way by henkma. If an auxiliary function f in your answer uses an operator that isn’t used elsewhere in your answer, and f is called once, make the operator an argument of f. This: (!)=take f a=5!a++3!a reverse.f Is two bytes longer than this: f(!)a=5!a++3!a reverse.f take Anders Kaseorg 06/20/2017. # Shorter syntax for local declarations Sometimes you need to define a local function or operator, but it costs lots of bytes to write where or let…in or to lift it to top-level by adding extra arguments. g~(a:b)=2!g b where k!l=k:take(a-1)l++(k+1)!drop(a-1)l g~(a:b)=let k!l=k:take(a-1)l++(k+1)!drop(a-1)l in 2!g b g~(a:b)=2!g b$a;(k!l)a=k:take(a-1)l++((k+1)!drop(a-1)l)a

Fortunately, Haskell has a confusing and seldom-used but reasonably terse syntax for local declarations:

fun1 pattern1 | let fun2 pattern2 = expr2 = expr1

In this case:

g~(a:b)|let k!l=k:take(a-1)l++(k+1)!drop(a-1)l=2!g b

You can use this syntax with multi-statement declarations or multiple declarations, and it even nests:

fun1 pattern1 | let fun2 pattern2 = expr2; fun2 pattern2' = expr2' = expr1
fun1 pattern1 | let fun2 pattern2 = expr2; fun3 pattern3 = expr3 = expr1
fun1 pattern1 | let fun2 pattern2 | let fun3 pattern3 = expr3 = expr2 = expr1

It also works for binding variables or other patterns, though pattern guards tend to be shorter for that unless you’re also binding functions.

2 Laikoni 06/21/2017
This also works inside list comprehensions: [f 1|let f x=x+1].

xnor 04/01/2016.

## Bind using guards

When defining a named function, you can bind an expression to a variable in a guard. For example,

f s|w<-words s=...

does the same as

f s=let w=words s in ...
f s=(\w->...)$words s Use this to save on repeated expressions. When the expression is used twice, it breaks even at length 6, though spacing and precedence issues can change that. (In the example, if the original variable s is not used, it's shorter to do g w=... f=g.words but that's not true for binding more complex expressions.) ##### 2 comments Lynn 08/09/2016 Isn’t this sort of a duplicate/special case of this answer? xnor 08/10/2016 @Lynn Looking back, it's a special case, but when I read that answer, the Just example made me think it's for pattern-matching to extract from a container, rather than to store on an expression. xnor 10/23/2016. ## Shorter conditional last$x:[y|b]

is equivalent to

if b then y else x

Here's how it works:

             [y|b]   x:[y|b]   last$x:[y|b] if... +-------------------------------- b == False | [] [x] x b == True | [y] [x,y] y ##### 4 comments Christian Irwan 10/23/2016 Should it be if b then y else x? xnor 10/23/2016 @ChristianIrwan Good catch, yes. Potato44 06/20/2017 Wouldnt using bool be shorter as you don't need a list comprehension xnor 06/20/2017 @Potato44 That's in Data.Bool, which costs bytes to import. BlackCap 04/17/2017. # Lambdabot Haskell A language is defined by its implementation, and lambdabot (the IRC bot over at #haskell) imports a ton of common modules by default. No need to spend precious bytes re-implementing your favorite functions from Data.List or Control.Monad, just write Haskell (Lambdabot) instead of Haskell in the title and you're good to go! Edit: Here's a list of stuff that it imports by default, which includes, among other things, Control.Arrow, Data.Bits, Data.Ratio, System.Random and {-# LANGUAGE ParallelListComp #-} - go wild! xnor 08/09/2016. ## Get suffixes Use scanr(:)[] to get the suffixes of a list: λ scanr(:)[] "abc" ["abc","bc","c",""] This is much shorter than tails after import Data.List. You can do prefixes with scanl(\s c->s++[c])[]. λ scanl(\s c->s++[c])[] "abc" ["","a","ab","abc"] This is longer but still beats the import. Pointfree is the same length: scanl((.pure).(++))[] Zgarb 04/13/2017. ## Partition a string with mapM and words This function computes all partitions of a given string into nonempty contiguous substrings: map(words.concat).mapM(\c->[[c],c:" "]) The idea is that the mapM non-deteministically replaces each character c with either "c" or "c ", and the resulting lists are concatenated and split at spaces. There are two gotchas: the string must not contain spaces (if it contains spaces but not line breaks, use "\n" and lines for one extra byte), and each partition occurs twice in the resulting list (with and without a trailing space, which gets eaten by words). I've used this technique a couple of times (at least here, here and here). It's pretty flexible, since you can apply more functions after words to modify the partitions, and/or replace map with another iteration function, like any. ##### 2 comments 1 xnor 11/04/2016 Great idea using string function to split! I'll have to use this sometime. The best general-purpose alternative I found was 45 bytes, 6 longer: foldr(\h t->map([h]:)t++[(h:y):z|y:z<-t])[[]]. Laikoni 04/24/2017 When applied directly to some x this can be slightly shortened to words.concat<$>mapM(\c->[[c],c:" "])x .

Laikoni 06/21/2017.

# Online Tools

• Try it online!
TIO supports online compilation of Haskell code with the GHC 8.0.2 compiler. As TIO is developed with code golfing in mind it's not just an online interpreter but also offers features like byte count, header and footer sections that do not count towards the total byte count (put your main and test code there), automatic markdown generation for a code golf submission, and more.

• pointfree.io
Converts Haskell code to pointfree Haskell code which sometimes is shorter, see this tip.
Note: When dealing with functions that take two or more arguments, the pointfree version generated by pointfree.io often includes the ap function which is not in Prelude. However <*> is an equivalent inline version of ap and contained in Prelude in ghc 7.10 or higher.

• Hoogle is a Haskell API search engine, which allows you to search many standard Haskell libraries by either function name, or by approximate type signature.

Useful to quickly search the documentation or lookup in which packages a function is included.

Zgarb 01/18/2017
Note that TIO currently uses GHC 7.8, so the stuff in this tip is not available.
Laikoni 01/18/2017