Tips for golfing in F#

21

7

What general tips do you have for golfing in F#? I'm looking for ideas that can be applied to code golf problems in general that are at least somewhat specific to F# (e.g. "remove comments" is not an answer). Please post one tip per answer.

ProgramFOX

Posted 2013-12-21T09:29:45.147

Reputation: 8 017

Answers

9

Use function instead of match when possible; it'll save 6 characters for 1-character variables:

let f=function // ... (14 chars)

vs

let f x=match x with // ... (20 chars)

It can also replace any pattern match to consistently save 1 character:

match a with|          // ... (13 chars)
a|>function|           // ... (12 chars)
(function| (* ... *))a // (12 chars)

Jwosty

Posted 2013-12-21T09:29:45.147

Reputation: 3 530

8

Need to use a method on variable for which you haven't yet constrained the type? Just compare it against a literal of the type you want it to be then throw away the result to annotate that variable's type:

let f (x:string)=x.Length
let f x=x="";x.Length

Jwosty

Posted 2013-12-21T09:29:45.147

Reputation: 3 530

7

Use the prefix notation for infix operators when you can - it'll save you from having to define a function to use them.

For example, you can turn this:

List.map(fun i->i+2)[1;1;2;3;5;8]

into this:

List.map((+)2)[1;1;2;3;5;8]

Roujo

Posted 2013-12-21T09:29:45.147

Reputation: 353

1

I use it here thank you!

– aloisdg moving to codidact.com – 2017-11-03T23:44:09.383

5

Tuple deconstruction

In case you can't get around to using variables, use tuple deconstruction instead of multiple let expressions

let a,b ="",[]

instead of

let a=""
let b=[]

Reading from stdin

F# core library defines an alias for System.Console.In called stdin. These allow you to read input.

// Signature:
stdin<'T> :  TextReader

TextReader on msdn

The big advantage aside the fact that it's shorter than Console is, you don't have to open System either

Iterating over string

string is basically a char seq, this allows you to use Seq.map directly with strings. It's also possible to use them in comprehensions [for c in "" do]

Mutables/Reference cells

Using reference cells is not always shorter as every read operation comes with an additional character to deref the cell.

General tips

  • It is possible to write the complete match .. with inline

    function|'a'->()|'b'->()|_->()
    
  • There is no need for white-space before and after non alphanumeric characters.

    String.replicate 42" "
    if Seq.exists((<>)'@')s then
    if(Seq.exists((<>)'@')s)then
    
  • In case you need to left or right pad a string with spaces, you can use [s]printf[n] flags for that.

    > sprintf "%20s" "Hello, World!";;
    val it : string = "       Hello, World!"
    

    Core.Printf Module

Brunner

Posted 2013-12-21T09:29:45.147

Reputation: 331

4

Use id instead of x->x

id is an operator standing for the identity function.

let u x=x|>Seq.countBy (fun x->x)

can be written

let u x=x|>Seq.countBy id

source

I use it here

aloisdg moving to codidact.com

Posted 2013-12-21T09:29:45.147

Reputation: 1 767

3

Eta-conversion for functions

Many thanks to Laikoni for this tip in one of my solutions.

Consider a function to, say, sum a string with 3 for upper-case letters and 1 for all other characters. So:

let counter input = Seq.sumBy (fun x -> if Char.IsUpper x then 3 else 1) input

By eta-conversion this can be re-written as:

let counter = Seq.sumBy (fun x -> if Char.IsUpper x then 3 else 1)

and called in the same way as before:

counter "Hello world!" |> printfn "%i"

The function forward-composition operator >>

Now suppose our original challenge would be to sum a string with 3 for upper-case letters and 1 for lower-case letters, and all other characters are excluded.

We might write this as:

let counter input = Seq.filter Char.IsLetter input |> Seq.sumBy (fun x -> if Char.IsUpper x then 3 else 1)

We can use the forward-composition operator (>>) to chain the two functions (Seq.filter and Seq.sumBy) together. With eta-conversion the function definition would become:

let counter = Seq.filter Char.IsLetter >> Seq.sumBy (fun x -> if Char.IsUpper x then 3 else 1)

Chris Smith did a great write-up on the >> operator on his MSDN blog.

Ciaran_McCarthy

Posted 2013-12-21T09:29:45.147

Reputation: 689

2

Avoid parenthesis when using one parameter and on tuple

let f = [(0,1);(1,4)]|>Seq.map(fst)
printfn "%A" f

can be written

let f = [0,1;1,4]|>Seq.map fst
printfn "%A" f

aloisdg moving to codidact.com

Posted 2013-12-21T09:29:45.147

Reputation: 1 767

1You also don't need () around tuples:

let f=[0,1;1,4]|>Seq.map fst – thinkbeforecoding – 2019-02-19T13:32:13.753

1Thank you. updated. – aloisdg moving to codidact.com – 2019-02-19T13:53:11.413

2

When possible Seq is shorter than List:

[[1];[2;3];[4];[5]|>List.collect
[[1];[2;3];[4];[5]|>Seq.collect

is one char shorter...

thinkbeforecoding

Posted 2013-12-21T09:29:45.147

Reputation: 131

2

Prefer new line string over "\n"

This will start to pay off at even a single new line character in your code. One use case might be:

(18 bytes)

string.Concat"\n"

(17 bytes)

string.Concat"
"

Inspired from Chiru's answer for es6.

Used here

aloisdg moving to codidact.com

Posted 2013-12-21T09:29:45.147

Reputation: 1 767

1

Use .NET

.NET offers a lot of nice builtins. F# can use them, so dont forget them!

Example:

open System.Linq

It can be helpful!

aloisdg moving to codidact.com

Posted 2013-12-21T09:29:45.147

Reputation: 1 767

1

Use lambdas to save a byte. For example, this:

let f x=x*x

Can be expressed as this:

fun x->x*x

dana

Posted 2013-12-21T09:29:45.147

Reputation: 2 541

1

Use for...to instead of for...in to walk a range

for i in[0..2]
for i=0 to 2

aloisdg moving to codidact.com

Posted 2013-12-21T09:29:45.147

Reputation: 1 767

1

The module keyword can be used to shorten module names when used repeatedly. For example:

Array.fold ...
Seq.iter ...
List.map ...

can become

module A=Array
A.fold ...
module S=Seq
S.iter ...
module L=List
L.map ...

This is more useful for longer programs where module methods are used repeatedly (and must be fully named each time because they have the RequireQualifiedAccess modifier), and allows shaving a few chars off especially when it's more useful to use a regular CLR array (e.g., mutability) than an F# seq or list.

LSM07

Posted 2013-12-21T09:29:45.147

Reputation: 191