5 ms·
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 equalit
by sasmithjr 3y ago
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.
- shortrounddev2 3y agoAssume 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.
- trealira 3y ago> 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 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.
- FrustratedMonky 3y agoThis is the biggest hurdle for a lot of people, at least for me. The immutability. 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.
- shortrounddev2 3y agoI got the concept of immutability, but I inevitably ended up recreating OOP patterns whenever I tried working with F#. I do Asp.net WebAPI Projects and trying to do dependency injection ends up just translating C# to F# code rather writing idiomatic F#. I just don't know if HTTP is something F# is really good at doing. I can see it being really useful if you were writing something math-heavy
- tremon 3y agoF# is severely hampered by its standard library, which is object-oriented by design. Some newer things like fluent api's or object initializers are more amenable to F#'s functional style, but on the whole my experience matches yours: as soon as you start interfacing with any .net library, it feels like you're writing in a foreign language.
- xupybd 3y agoI make web apps with the SAFE stack. Using idiomatic F#, the experience has been great. 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/ https://safe-stack.github.io/
- phillipcarter 3y ago> I just don't read code as I would a mathematical proof. 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.
- trealira 3y agoTo be honest, when I read e.g. Haskell code, I don't get the sense of a mathematical proof at all, not compared to proof assistants like Isabelle/HOL, Coq, or Lean. It looks more like abstract, high-level wizardry, casting that transform the output of one function into the correct type so that it can become input into another; and yet it's very abstract typing, nothing like proof-assistant tactics (though I admit I'm not all that experienced with proof assistants either). I think immutability is more about making it easier to reason about the code than mathematical proofs specifically.
- freilanzer 3y ago> That's fine, although I'd say that FP is probably just not for you. That's just gatekeeping.
- phillipcarter 3y agoIt’s not, but you’re free to believe that.