News for hackers. Stuff that matters.
Re:Grain: A strongly-typed functional programming language f... by faichai on Sunday April 25, 2021 @06:34AM (#26931697)
The way recursive functions are defined seems strange [1].

The are declared using `let rec foo = ()=> ...` syntax. So the modifier is on the variable declaration rather than the function definition. Since recursiveness is a property of the function this seems like the wrong way round. JavaScript gets this right with `async`.

[1] https://grain-lang.org/docs/guide/functions

Re:Grain: A strongly-typed functional programming language f... by kqr on Sunday April 25, 2021 @06:43AM (#26931745)
No, in this case I think you're mistaken. "Rec" means whatever is the definition of the variable can reference the variable itself.

In other words, the default behaviour is that the scope, during variable definition, does not include the variable being defined. With "rec" the variable being defined is added to the scope in the definition of the variable itself.

Coming from lambda calculus, this makes sense: functions themselves cannot be recursive. What makes them recursive is taking their own definition as a parameter, I.e. getting their name in their scope.

Re:Grain: A strongly-typed functional programming language f... by exDM69 on Sunday April 25, 2021 @08:13AM (#26932135)
let vs. let rec is in ML and Scheme and some other languages. But the compiler could transform the let rec into a let, and e.g. Haskell does it so the programmer does not have to.

It's not exactly a trivial algorithm as it involves finding strongly connected components from the dependency graph and then topologically sorting that.

I implemented this for a toy programming language compiler project, it was a fun one. Luckily I had SCC and topological sort from Haskell's Data.Graph module.

Re:Grain: A strongly-typed functional programming language f... by pjmlp on Sunday April 25, 2021 @03:19PM (#26935688)
Haskell does it, because being lazy evaluated helps quite a lot in such algorithms.
Re:Grain: A strongly-typed functional programming language f... by christophilus on Sunday April 25, 2021 @07:28AM (#26931936)
This is pretty common in the ML line of languages. Here’s F# doing the same thing: https://docs.microsoft.com/en-us/dotnet/fsharp/language-refe...
Re:Grain: A strongly-typed functional programming language f... by faichai on Sunday April 25, 2021 @08:20AM (#26932170)
Ahh, this makes sense. Thank you for the link. I was thinking it was more of modifier to switch on tail call optimisation (or something) rather than a scoping modifier.