Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F# list spread over multiple lines

Ok, I have a List of functions in F#, and for claritry i want to spread them over multiple lines, as shown:

type pos=char*int
let potentialPoss:List<pos->pos>=
    [fun (c,i)->(c+char 1,i+2);
    fun (c,i)->(c+char -1,i+2);
    fun (c,i)->(c+char 1,i-2);
    fun (c,i)->(c+char -1,i-2);
    fun (c,i)->(c+char 2,i+1);
    fun (c,i)->(c+char -2,i+1);
    fun (c,i)->(c+char 2,i-1);
    fun (c,i)->(c+char -2,i-2) ]

Seems reasonable enough right? (FYI, pos stores the algebracic position of a chess piece, and potentialPoss is a list of knights moves)

But I get a syntax on the second fun :"Unexpected keyword 'fun' in expression. Expected ']' or other token." and on the [: "Unmatched '['."

this feels like hte right syntax to me? any sugestions?

like image 828
Lyndon White Avatar asked Aug 23 '26 23:08

Lyndon White


2 Answers

You just need all the list elements to be as indented as the first element:

[fun (c,i)->(c+char 1,i+2);
 fun (c,i)->(c+char -1,i+2);
 fun (c,i)->(c+char 1,i-2);
 etc

This is true for all blocks of code (computation expressions, pattern matching, etc).

like image 149
dahlbyk Avatar answered Aug 26 '26 04:08

dahlbyk


I'm not sure why the compiler barfs on this, but this fixes it:

type pos=char*int
let potentialPoss:List<pos->pos>=
    [
        fun (c,i)->(c+char 1,i+2)
        fun (c,i)->(c+char -1,i+2)
        fun (c,i)->(c+char 1,i-2)
        fun (c,i)->(c+char -1,i-2)
        fun (c,i)->(c+char 2,i+1)
        fun (c,i)->(c+char -2,i+1)
        fun (c,i)->(c+char 2,i-1)
        fun (c,i)->(c+char -2,i-2) 
    ]

Incidentally, semicolons are generally interchangeable with line breaks in F#, so I removed them.

like image 27
Daniel Avatar answered Aug 26 '26 03:08

Daniel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!