by sasmithjr · 3y ago · 0 comments
a <- 4 // Assignment, valid
This is a compiler error because you haven't made your variable mutable.Also, I actually like that initial assignment and equality testing both use =. I think of `let a = 1` to be less like assignment and more like `Assume that a = 1 is true`, so using the same operator (e.g. `a = 3`) makes sense for comparisons.
And `<-` as a separate operator is good because mutability should be exceptional for many (most?) codebases.
Assume that a = 1 is true
I just don't read code as I would a mathematical proof. I think in terms of what memory locations are equal to what in the stack, or the heap, and what is the lifecycle of that data in that memory address.When I read "int a = 1" in C#, I implicitly translate that to "take a 4 byte piece of memory on the stack, and set it equal to 1". I don't think in the abstract sense of a formula.
When I see a class like:
class Foo {
int x = 2;
string xyz = "Hello";
}
var foo = new Foo();
I read this as "allocate a chunk of memory big enough in the heap to insert a 4 byte integer and an 8 byte pointer. Set that 8 byte pointer equal to a static chunk of memory where the "Hello" string is pooled.I think that's overspecifying a bit. It could be kept in a register rather than the stack. And due to to the Single Static Assignment transformation that modern optimising compilers do, variables don't correspond exsctly to registers anymore; each time you modify the variable, it becomew a new variable, and then the compiler removes or changes extraneous modifications and dead code. It only keeps track of the values that move through the code. You could really only count on variables corresponding exactly to stack space registers before the SSA form existed.
That's fine, although I'd say that FP is probably just not for you. It's very much a style of programming that lends itself more towards "programming is akin to proofs" than "programming is about manipulating things on a von neumann architecture". Neither is an incorrect view of the world, but they do represent different ways of reasoning about things and it's better to use a language more suited towards one way of thinking.
I think immutability is more about making it easier to reason about the code than mathematical proofs specifically.
That's just gatekeeping.
IF you have grown up doing objects, or C#, and thinking with variables. Then 'a=1' means a memory for variable a has a 1, and you should be able to change that. But it is really a like a function where the function returns a 1.
'let a = 1' is not assigning the value 1 to variable a.
'a' is a function that returns a 1.
I think this is biggest reason why people trying to learn functional programming in languages that don't enforce immutability, have a harder time than with languages that do enforce it.
Like moving to another country, and the people around you purposely don't speak English so you have to learn the language. If the did speak English to help you, then you wouldn't learn the language.
Enforcing immutability is like this.
Mutable variables are better explained as slots or cells, I think. Both OCaml and Rust have a concept of ref cells, for example (and they entirely replace mutable local variables in OCaml).
1: https://en.m.wikipedia.org/wiki/Variable_(mathematics) 2: https://en.m.wikipedia.org/wiki/Variable_(computer_science)
I can imagine that trying to translate C# to F# would be horrible. That is going against the grain.
I'd encourage you to have a look at the SAFE stack it's really nice to use. https://safe-stack.github.io/
In any case. I get it. If you have done ASP.NET, and C#, then suddenly this F# way of building web pages by programming through combining functions, it is hard to get over the hump. Like brain has to re-change how to think through the whole flow. Really, F# on the web is like ELM.
I like https://websharper.com/
but other use this https://suave.io/