by shortrounddev2 · 3y ago · 0 comments
let a: int = 1 // Assignment
a = 3 // Testing equality, will give you an error
a <- 4 // Assignment, valid
There's an article here with more things:https://alexyakunin.medium.com/crying-out-loud-what-i-like-d...
I want to like F# since I love C#, but I just want something closer to Scala, but for .Net
* Linearly ordered files. Several times in F# I have tracked down an issue simply by bisecting the codebase, which is impossible in C# because there's no meaningful way to halve the code. I think I've only once ever had a problem with the linear ordering that wasn't solved simply by reordering some files.
* Explicit type conversions. I've only ever found this awkward when interfacing with C# code that is designed around C#'s extreme laxity. Explicit is better than implicit!
* "No struct tuples" is false - that's what the `struct` keyword is for. (It may have been true when the article was written.)
* Dot notation for indexing is now no longer necessary.
It's certainly true that OO idioms are often clunky and feel strangely like they were constructed by Frankenstein, but then I almost never find myself trying to use them anyway. You don't notice oddities in features which you never think of using! Similarly, the number of times I use `<-` is so low that I don't think of it as being incongruously odd - why shouldn't there be a baroque syntax to indicate the place in your code that's likely to have a 50% higher chance of bugs?
Alternatively you can take the more functional approach and rewrite your function logic to use `if x then (value) else` throughout instead of `return (value)` or `yield (value)`. Early return isn't really a thing in f# but if-else is.
[0] https://fsharpforfunandprofit.com/posts/computation-expressi...
Quite probably. The underlying CLR ValueTuple type is a relatively recent .NET addition (and partly only exists because C# asked for it). (It was added in Fx 4.7 / Core 1.0, whereas the reference type Tuple was added way back in Fx 4.0.)
That's what I have been working toward with my language-ext library [1]. Including a ZIO like effects system [2], Haskell-like Pipes [3], Clojure-like concurrency primitives [4], the fastest immutable data-structures in .NET [5], and lots of other common FP bits.
Obviously more support for expression based programming would be welcome (and higher kinds), but you can do a lot with LINQ and a good integrated library surface.
[1] https://github.com/louthy/language-ext
[2] https://louthy.github.io/language-ext/LanguageExt.Core/Effec...
[3] https://louthy.github.io/language-ext/LanguageExt.Core/Effec...
[4] https://louthy.github.io/language-ext/LanguageExt.Core/Concu...
[5] https://louthy.github.io/language-ext/LanguageExt.Core/Immut...
"This is a C# doc-gen tool. It was primarily built to support my Language-Ext project. Which is non-idiomatic in its approach. I couldn't find documentation generators that did it justice. I also really liked the Hackage documentation style from Haskell, so have taken a styling approach from there (even if it looks a little dated now, it was always the documentation I felt most comfortable reading)."
language-ext isn't idiomatic C# and so idiomatic C# doc-gens have quite a bad time with this library. One of the major benefits over other doc-gens is it grabs the README.md from each folder and prepends it to the relevant section - allowing me to write some contextual documentation outside of the auto-generated API documentation. This makes it a much more approachable reference.
It also supports markdown in the XML documentation, avoiding a lot of the need for lots of ugly XML special characters [in code] and extending the layout options compared to the common C# doc-gens.
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)
let a: int = 1
This isn't assignment; it's a binding that permanently associates the identifier `a` with the value `1`.Recently I did some F# conversion to Python, and the Python code really did seem ugly and ungainly in comparison.
> That’s it for now. Hopefully, some of these issues will be addressed in future versions of F#, though as I said, most of this is simply annoying / inconsistent. It shouldn’t stop anyone from using this awesome language
and as other have pointed out, some of this is complaining about features or not working with the language. File order matters should probably exist in more languages (the amount of debugging/code reading time it saves is insane) and complaining about explicit typing because it's slowing down your contest code is just eye rollingly silly to me.
What about it is lacking?
I'm asking because at one time I was weighing moving to Scala, but the F# algebraic types and compiler seemed more complete.
Is it syntax? that does take some getting used to.
The only problem with it is that you can't write things like:
let x^2 + y^2: float = 1
In early versions of F# you had to stick `#light` at the top of a file to use lightweight syntax, which is now the default. The verbose syntax, which is closer to ML was originally the default.
> 1. You have to manually order your files in F# projects in order they must be processed by the compiler.
This is one of my favourite features of F# because it forces you to design your code in a way that minimizes mutually recursive types, since all mutually recursive types must live in the same code file. You end up with cleaner code which is easier to test, maintain and reason about.
> 2. No break and continue for loops. 3. No do-while loops.
I've never seen as a problem because I rarely write loops. It's so rare that I use them I end up having to double-check the syntax. I always reach to .map, .fold, .filter, etc first, and I can't remember a time I couldn't express what I wanted using these.
> 6. Awful type constructor syntax
The primary constructor syntax makes sure all other constructors must call it. Prevents you from not instantiating a class incorrectly and is a better fit for immutable-first types. Perhaps could have some improvements but the common case becomes more terse to write.
> 7a. a) array type can be declared as “int[]” or “int array”; same for list and seq; moreover, it’s the same for any other generic type with one type argument.
Comes from ML, where multi-parameter generics are also written `(x * y) type`. I think it was a good idea to make F# closer to .NET and use `Type<X, Y>`, but they should have done the same for single-parameter generics too to make it consistent. I never use `X array` syntax and prefer `array<x>`, though I would prefer if they also made these case consistent and I could write `Array<x>`, but Array is a module. Not sure how to fix this.
> 8. “fun x y z -> …” syntax for lambda expressions
Again from ML. I imagine there are syntactic ambiguities if you drop the `fun`, as `->` is also used in pattern matching and other places.
> 9. a) <expression>[<expression>] = <indexingExpression> ... Please, please implement this.
I dislike the idea of spaces being significant, and there are already gotchas in F# where this is the case. `x().y` and `x ().y` are not always the same thing.
> 12. “<-” for assignment. Another personal thing, maybe, but “:=” seems easier to type.
`:=` was used for ref cell assignment, but is now deprecated.
But I agree with some ad hoc words criticism
shadowing would be a second 'let a = 2'
That confused me, as I assumed that code there is compiling. But the only way for it compile is shadowing, so yeah, I kind of assumed that to shadow is to use this syntax. I'm not working with F# for some time, so should've checked before posting.