Haskell, 26 bytes
Try it online!
In non-pointfree notation and using concatMap instead of =<<, this becomes
f s = concatMap(\c-> show c ++ ":")s ++ ""
Given a string s, we map each char c to a string "'c':" using the show function which returns a string representation of most Haskell types. ...
Define infix operators instead of binary functions
This saves usually one or two spaces per definition or call.
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 ...
Haskell, 33 28 26 bytes
Try it online!
fold the given pointfree function from the right into the input string starting with . The function is: show char as a Haskell char, i.e. surrounded with ' and concatenate with the result so far after putting a : in front of it.
Edit: @Ørjan Johansen saved two bytes. Thanks!
Use guards not conditionals:
f a=if a>0 then 3 else 7
Use semicolons not indents
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)
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 ...
Use the list monad
A quick review:
xs >> ys = concat $ replicate (length xs) ys
xs >>= f = concatMap f xs
mapM id[a,b,c] = cartesian product of lists: a × b × c
mapM f[a,b,c] = cartesian product of lists: f a × f b × f c
Repeating a list twice
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&...
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
is quite a bit shorter. It is in the Prelude so no need for imports!
MagicHash, 30 bytes
-XMagicHash outputs 1, -XNoMagicHash outputs 2
MagicHash allows variable names to terminate in a #. Therefore with the extension, this defines two functions y# and x# which each take a value and return a constant 2, or 1. x#x will return 1 (because it is x# applied to 1)
Without the extension, this ...
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]]
Or filter (and optionally a map at the same time):
Prelude> [show x|x<-[1..60],mod 60x<1]
CPP, 33 20 bytes
Prints 0 with -XCPP and 1 with -XNoCPP.
With -XCPP, a slash \ before a newline removes the newline, thus the code becomes main=print$0-- +1 and only 0 is printed as the +1 is now part of the comment.
Without the flag the comment is ignored and the second line is parsed as a part of the previous line because it is ...
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:
Haskell, 169 148 bytes
l%('(':r)|(y,x:s)<-%r,m<-y:l=last$m%(p:s):[(p:p:(l>>k)++x:foldl(\r x->x++[' '|x>k,r>k]++r)[x]m,s)|x<',']
Try it online! Takes the tuple as a string. init.tail.fst.(%) is the anonymous main function. Bind it to e.g. f and use like f "(3,(14,1),4,7)", which yields "(,...
Match a constant value
A list comprehension can pattern match on a constant.
This extracts the 0's of a list l, i.e. makes a list of as many 0's as are in 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.
Know your monadic functions
1)simulate monadic functions using mapM.
a lot of times code will have sequence(map f xs), but it can be replaced with mapM f xs. even when just using sequence alone it is longer then mapM id.
2)combine functions using (>>=) (or (=<<))
the function monad version of (>>=) is defined as so:
(f >>= g) x = g (f ...
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.
Is two bytes longer than this:
Use words instead of a long list of strings. This isn't really specific to Haskell, other languages have similar tricks too.
words"foo bar" -- 1 byte longer
words"foo bar baz" -- 1 byte shorter
words"foo bar baz qux" -- 3 bytes shorter
BinaryLiterals, 57 bytes
instance Show(a->b)where;show _=""
-XBinaryLiterals prints a single newline. -XNoBinaryLiterals prints a 1.
I am sure there is a better way to do this. If you find one, please post it.
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.
To negate an expression e, just do -e. For example, -length[1,2] gives -2.
If e is even moderately complex, you will need ...
is equivalent to
if b then y else x
Here's how it works:
[y|b] x:[y|b] last$x:[y|b]
b == False |  [x] x
b == True | [y] [x,y] y
Use pattern guards
They're shorter than a let or a lambda that deconstructs the arguments of a function you're defining. This helps when you need something like fromJust from Data.Maybe:
f x=let Just c=… in c
is longer than
f x=(\(Just c)->c)$…
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 ...
MonomorphismRestriction + 7 others, 107 bytes
This uses TH which requires the flag -XTemplateHaskell at all times.
File T.hs, 81 + 4 bytes
module T where
Main, 22 bytes
Compiling with the flag MonomorphismRestriction forces the type of p to Integer -> ...
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:
This is a recursive ...
Don't use backticks too often.
Backticks are a cool tool for making sections of prefix functions, but can sometimes be misused.
Once I saw someone write this subexpression:
Although it is the same as just v x.
Another example is writing (x+1)`div`y
as opposed to div(x+1)y.
I see it happen around div and elem more often because these functions are ...