Semicolon in Ocaml


  • ♿ (Parody)

    What is it and why should anyone care?

    What's the deal with this?

    ∀α . ∀β . α → β → β function.

    What's interesting about that?

    Asking ( @Gustav ) as a formally uneducated (at least for CS) rube.


  • Banned

    @boomzilla think: C's comma operator. But implemented as a regular function without any compiler magic.

    BTW, that's just type signature, not actual implementation. But mathematically, there can be only one implementation that matches that signature. It's impossible to come up with another one without adding additional constraints on β.


  • ♿ (Parody)

    @Gustav so why is it cool? Would you be using commas in weird places in C? Mostly I have used them when calling a function or declaring multiple variables. Occasionally in a for loop to do multiple things.

    What sort of coolness does this give you when writing Ocaml (a language I've never used, BTW)?


  • Banned

    @boomzilla in C, you separate each consecutive statement in a function with ;. You can't do that in OCaml because every function is made of a single expression. An if statement is expression, a switch statement is expression, variable declaration is expression, even a nested named function is an expression. The ; operator is also an expression, but functionally it allows you to write code as if there were multiple ;-separated statements.

    It's rarely used but pretty useful when needed, but mostly it's just mathematically beautiful. The entire code, start to finish, without any extra bits behind the scenes, is let (;) a b = b; - and it enables a whole new programming paradigm (imperative code in normally functional-only language).


  • BINNED

    @Gustav said in Semicolon in Ocaml:

    BTW, that's just type signature, not actual implementation. But mathematically, there can be only one implementation that matches that signature. It's impossible to come up with another one without adding additional constraints on β.

    Why is that?

    Taking a WAG to answer my own question, I assume that's because identity is the only operation you can perform on all types "without adding additional constraints". That is, likewise, ∀α . α → α only has the single implementation let id a = a; since any other endomorphism (*cough*), for example, let successor a = a+1; would put a constraint on what inputs the function can take.

    Close or missed the mark?


  • Java Dev

    @boomzilla said in Semicolon in Ocaml:

    @Gustav so why is it cool? Would you be using commas in weird places in C? Mostly I have used them when calling a function or declaring multiple variables. Occasionally in a for loop to do multiple things.

    What sort of coolness does this give you when writing Ocaml (a language I've never used, BTW)?

    Neither of those are actually the comma operator.


  • ♿ (Parody)

    @Gustav said in Semicolon in Ocaml:

    @boomzilla in C, you separate each consecutive statement in a function with ;. You can't do that in OCaml because every function is made of a single expression. An if statement is expression, a switch statement is expression, variable declaration is expression, even a nested named function is an expression. The ; operator is also an expression, but functionally it allows you to write code as if there were multiple ;-separated statements.

    It's rarely used but pretty useful when needed, but mostly it's just mathematically beautiful. The entire code, start to finish, without any extra bits behind the scenes, is let (;) a b = b; - and it enables a whole new programming paradigm (imperative code in normally functional-only language).

    Ah, OK, a workaround for the functional programming dildo of unintended uselessness.


  • ♿ (Parody)

    @PleegWat said in Semicolon in Ocaml:

    @boomzilla said in Semicolon in Ocaml:

    @Gustav so why is it cool? Would you be using commas in weird places in C? Mostly I have used them when calling a function or declaring multiple variables. Occasionally in a for loop to do multiple things.

    What sort of coolness does this give you when writing Ocaml (a language I've never used, BTW)?

    Neither of those are actually the comma operator.

    Where would you use the "comma operator" then?


  • Banned

    @topspin bingo. Semicolon operator is basically identity function with an extra, unused argument.


  • Banned

    @boomzilla said in Semicolon in Ocaml:

    @PleegWat said in Semicolon in Ocaml:

    @boomzilla said in Semicolon in Ocaml:

    @Gustav so why is it cool? Would you be using commas in weird places in C? Mostly I have used them when calling a function or declaring multiple variables. Occasionally in a for loop to do multiple things.

    What sort of coolness does this give you when writing Ocaml (a language I've never used, BTW)?

    Neither of those are actually the comma operator.

    Where would you use the "comma operator" then?

    The most common use is in the third part of the for loop, when you want to update multiple variables at once.


  • ♿ (Parody)

    @Gustav said in Semicolon in Ocaml:

    @boomzilla said in Semicolon in Ocaml:

    @PleegWat said in Semicolon in Ocaml:

    @boomzilla said in Semicolon in Ocaml:

    @Gustav so why is it cool? Would you be using commas in weird places in C? Mostly I have used them when calling a function or declaring multiple variables. Occasionally in a for loop to do multiple things.

    What sort of coolness does this give you when writing Ocaml (a language I've never used, BTW)?

    Neither of those are actually the comma operator.

    Where would you use the "comma operator" then?

    The most common use is in the third part of the for loop, when you want to update multiple variables at once.

    That's what I thought (though I've also used it in the beginning to initialize multiple things, hence my more general statement in my previous post), but @PleegWat says it's not.


  • Banned

    @boomzilla said in Semicolon in Ocaml:

    though I've also used it in the beginning to initialize multiple things

    That one is actually not a comma operator, but a special form of variable declaration syntax that also uses the comma character. Unless you're changing an existing variable, then yes, it's a comma operator then.


  • ♿ (Parody)

    @Gustav yes, I've done both.


Log in to reply