by crntaylor · 13y ago · 0 comments
match list with
| x::xs -> ...
with match list with
| head::tail -> ...
to make it clear what the names are referring to. But in F#, something to the left of :: can never be anything other than the head of a list, and something to the right of :: can never be anything other than the tail of a list! Calling them 'head' and 'tail' doesn't add any extra information. It just adds more characters.The names x and xs are entirely appropriate when you don't know anything about the values you're working with (not even their types). All you know is that x has a type 'a, and xs has the type 'a list. Giving them abstract names reflects the fact that you are writing an abstract function. If you are writing a more concrete function, use more concrete names (e.g. trade or blogPost or playerAction... but not head and tail!)
Also, I don't know any F#, but is he correct when he says that the order of declarations in a file matters, and that the order of files in a project matters? That sounds crazy to me. What is the benefit? Both Haskell and Ocaml get along fine without relying on declaration order to do type inference.
Other than that - good article, I agree!
When used well they often make code more readable than "descriptive" names, just like most English text is more readable with pronouns than without. In any case, categorically claiming they are bad makes about as much sense to me as banning pronouns from English.
> That sounds crazy to me. What is the benefit? Both Haskell and Ocaml get along fine without relying on declaration order to do type inference.
Here is great explanation on the benefits of this. http://cs.hubfs.net/topic/None/59219#comment-70220
IIRC in OCaml you have to compile modules in topological order wrt dependencies.
Yes, but the names you assign in a pattern get used outside the pattern. Presumably there will be an x/head or an xs/tail on the right side of the arrow in your example, and it's there that meaningful names help.
That said, I'm quite fond of the x:xs naming convention. It's particularly useful when you're doing something with two or more lists in the same scope, and you pattern-match to produce multiple heads and tails. For instance (Haskell syntax, as I don't know F#):
zip :: [a] -> [b] -> [(a, b)]
zip (x:xs) (y:ys) = x : y : zip xs ys
zip _ _ = []
This also illustrates when you might well disregard the trope of using "meaningful" names. The code above is polymorphic and will work for any types a and b, and for any values of those types, no matter what they represent. Functional programming really encourages this sort of generality. Yes, it's very much the case that, as the article's author wrote, "functional code tends to focus on applying generic transformations to 'things', and not that much on what the 'thing' might be."But, if it helps to clarify what you're doing at the domain level, you can (and should) assign meaning through types and by renaming and adding restrictive type signatures to polymorphic code:
data Child = ...
data Chore = ...
type Assignment = (Child, Chore)
assign :: [Child] -> [Chore] -> [Assignment]
assign = zip
This not only helps readers understand what's going on, it also enlists the type-checker in helping to ensure you've, say, assigned chores only to your kids and not to your pets. zip (x:xs) (y:ys) = (x, y) : zip xs ysIt is true that the order of files matters. Presumably this is for the same reason that order matters within a file.
He is correct. (At least, this was true a couple years ago. I haven't used F# since then, but I doubt this has changed.)
I don't have enough F# experience to speak to any potential benefits of this restriction.
If you're a Java or C# programmer, or even a C++ programmer, you're used to cyclic relationships all over the place. Your Person class has a .Adresses property and your Address class has a .Residents property so that you can conveniently person.Addresses.Filter(address => address.Residents.Count > 1) and suddenly your Address class is tightly coupled to your Person class. Do this a few dozen times and before you know it, your software is a maintenance and refactoring nightmare.
Object-oriented programming encourages this design. Functional programming discourages it. Modern OOP compilers conveniently resolve circular dependencies (Java does, C++ doesn't without header games). Modern FP compilers force you to declare and contain your circular dependencies.
Lastly, declare-before-use makes a lot of dynamic and live programming scenarios viable. Lisps, for example, generally have a compilation unit of a top-level form. This way the REPL and a normal source file behave more similarly, if not precisely the same.
It's just odd. Why can I declare co-dependent functions and types, but not modules?
I haven't been tracking closely but no Forward declarations was one of common complaints, along with organizing projects in subdirectoris (I think they have fixes for that now) and VS support in general
http://stackoverflow.com/questions/1378575/f-forward-type-de...
__________
Also Xamarin shd be releasing... soon
But I'd like to add that throughout almost all OCaml code I've written and used to read, that type of matching was written:
match list with
| h::t ->
Wich reads easily `h` for `head` and `t` for `tail`. I'm surprised it hasn't been reproduced in the F# world.`h::t` seems even better to me than `head`/`tail` or `x`/`xs` which for the second one, means nothing really to me.
Personally, I always use x::xs in one-liners and something meaningful (no, neither head nor h is meaningful) in longer matches.
H and t can also be found in the official Caml and batteries source code, along with hd/tl, a/l and probably x/xs if it pleases you.
I can't agree more about naming variables correctly where it makes sense though.
MyProj
|- Things.fs
|- Main.fs
Visual Studio has a few functions to manage this order.The benefit is faster compilation, because F# requires recursive types to be declared together, such as
type Foo = A of Bar | B
and Bar = {Baz: Foo}
this doesn't really come up that much.Unless, of course, if you don't know or expect that 'rule' and you are wondering, why your type is not visible in that other module for over an hour...
In my mind it's that code readability is at last being discussed, signals that F# might be nearing mainstream.
I use OCaml and I think of it as idiomatic naming for pattern matching on a list, in a similar way to how iteration variables are often named `i`, although `index` might be more superficially clear, but in reality it is not really since one "knows" `i` is the interation variable, just like `x::xs` means `x` is an element and `xs` is a list of x'es, that is elementy of the same type as x. Pretty clear in my opinion.
I agree with the GP that x::xs is more appropriate.
As for "fringe," that may or may not be true. It's pretty subjective. But really, who cares? Back when Ruby was the weird new thing gaining visibility, I heard the same complaints about its idioms. Last I checked, it's still doing fine. I'm OK using "fringe" languages like Ruby.
Optimizing code for people who don't know anything about the language basically means sticking as close to the C or Python way of doing things as possible. If your language is significantly different from C, this is obviously not going to be a global maximum of readability.
Readability to F# programmers is what matters, along with a host of other concerns, such as terseness.
And how is head::tail any better? That's like insisting for loops use "counter" instead of "i".
Being able to figure out a meaning of something is crucial in programming. We spend much more time reading the code than writing it and most of the time during reading is spent on resolving the meaning of symbols. To be able to tell what a symbol means, one needs to take into account many things - from OS it's running on to language it's written in to declared variables in the scope. And much more, of course.
Now back to your example: to be able to understand `x::xs` I need to know that `::` is a cons operator. Or more precisely, to understand the meaning of `x` symbol I need to take into account another symbol.
On the other hand of the spectrum is something like this: `first_element_of_list :: rest_of_list`. To understand the meaning of the first symbol in this expression you need to know exactly one symbol - itself. That's one benefit. The second is that someone who doesn't know what consing is, let alone how it's written in OCaml, can still understand this expression. So that's the second benefit.
It's worth remembering that we do not write the code for ourselves nor for the computers. We write the code to be read by other humans (always - don't argue). As with any kind of writing, then, it's helpful to know the audience. If I was writing a tutorial on constructing lists in OCaml (for beginners), I would use the second example above. But if I was writing production code (so for me and my team to read) I would use something shorter, like `head :: tail` for example. And if I was writing a quick and dirty script for my personal use, or if I was contributing to the work of hardcore OCaml hackers I would probably write `x :: xs` - because in case of script I'd be convinced that readability doesn't matter and in second case I would be certain that everyone reading and working with the code I wrote would be already so used to the construction that they would read it without problems.
That's another thing to consider: after using a language for a long enough time we tend to internalize its idioms; when this happens we are able to treat bigger constructs as one symbol. This ability helps in reading the code quickly, but it's worth remembering that not everyone developed it. Also, because longer names do not hinder quick-reading of idioms for those who know them, that's not a counterargument to longer names.
As for loops... Well, I do - sometimes - use `i` as a variable name, but I tend not to. Most often, after a while I come back to the code and replace `i` with `idx`. But to even use `i` as a name in the first place it needs to refer to the nonnegative integer value. In Python you generally iterate over collection directly, so you write `for what_is_it in a_collection:` instead of for example: `for i in 1 to 10: an_element = a_collection[i]`. Anyway, I use single letter variables very sparingly and in the smallest scope possible.
What I want to say, I guess, is that descriptive names are important in good programming and that there is really no argument for using non-descriptive ones.
What I see in "x::xs" is one simple expression at a glance. It is the cons that is the central concept in being able to understand the expression. So my next thought was to wonder what on earth someone is doing programming F# if they don't understand the first thing about working with lists? Cons is what makes the abstraction even possible. If you don't get that, longer symbols surely can't help except maybe to hide the magic gobbledygook.
You summarize your argument by saying descriptive names are important, but your examples do not support that at all. Descriptive names are important for things that need to be described. If I write a function "reverse_string" with a single string argument, that argument had damn well better be named "s". There is no longer name that can convey any useful additional information. Many longer names will be distracting, and make the code harder to read.