Multiple let statements on the same line in F#

module Digits
type Digit = Unison | Semitone | Tone | MinorThird | MajorThird | PerfectFourth | AugmentedFourth | PerfectFifth | MinorSixth | MajorSixth | MinorSeventh | MajorSeventh type 'd GeneralizedDigit = SmallDigit of 'd | Octave type 't Number = EmptyNumber | CountedNumber of 't * 't Number
let swapOctave: Digit GeneralizedDigit -> Digit GeneralizedDigit = fun x -> match x with SmallDigit Unison -> Octave | Octave -> SmallDigit Unison | g -> g
let limitLength: 'r Number -> Digit = fun a -> match a with EmptyNumber -> Unison | CountedNumber(_,EmptyNumber) -> Semitone | CountedNumber(_,CountedNumber(_,EmptyNumber)) -> Tone | CountedNumber(_,CountedNumber(_,CountedNumber(_,EmptyNumber))) -> MinorThird | _ -> MajorSeventh

In F# I can put multiple type definitions on the same line without semicolons without any problems, but when I remove the newline between the let statements I get the error FS0010. I know that in Haskell statements can be separated by a single semicolon but in F# neither a single semicolon nor a double semicolon will work. How do I have multiple let statements on the same line?

3 answers

  • answered 2022-01-13 05:09 Phillip Carter

    You can do this with the let .. in syntax like so:

    let f () = let a = 1 in let b = 2 in a + b
    f () // gives 3 as a result
    

    But I would really recommend against doing multiple single-line definitions like this. It's hard for people to read.

  • answered 2022-01-13 13:55 Tomas Petricek

    As explained by Phillip, the let .. in .. construct allows you to define a local variable as part of a one-line expression.

    However, your example seems to be trying to define multiple top-level definitions in a module, which is something you cannot achieve with let .. in ...

    As far as I can tell, you can actually do this by separating the definitions with two semicolons, i.e. ;;. If I save the following as test.fs and load it using #load, I get no errors:

    module Digits
    type Digit = Unison | Semitone | Tone | MinorThird | MajorThird | PerfectFourth | AugmentedFourth | PerfectFifth | MinorSixth | MajorSixth | MinorSeventh | MajorSeventh type 'd GeneralizedDigit = SmallDigit of 'd | Octave type 't Number = EmptyNumber | CountedNumber of 't * 't Number
    let swapOctave: Digit GeneralizedDigit -> Digit GeneralizedDigit = fun x -> match x with SmallDigit Unison -> Octave | Octave -> SmallDigit Unison | g -> g;; let limitLength: 'r Number -> Digit = fun a -> match a with EmptyNumber -> Unison | CountedNumber(_,EmptyNumber) -> Semitone | CountedNumber(_,CountedNumber(_,EmptyNumber)) -> Tone | CountedNumber(_,CountedNumber(_,CountedNumber(_,EmptyNumber))) -> MinorThird | _ -> MajorSeventh 
    

    I tested this in F# 5.0. It may be the case that this has changed in F# 6 which removed deprecated features like #light "off". The removal of ;; is not discussed in the post, but it may have been a related change. If that is the case, you may report it as a regression - but it is likely support for ;; should also be removed!

    As mentioned by Phillip, I do not see any reason for actually trying to do this.

  • answered 2022-01-13 15:19 David Raab

    If you want multiple let bindings to bound values to variables, then you also can use the "tuple syntax".

    let x,y,z = 1, "Hi", 3.0
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum