4 ms·
I've been learning Haskell lately but I've seen the same idiom in other functional languages. I was struck by two things you said: you have to know cons'ing to
by h_r 14y ago
I've been learning Haskell lately but I've seen the same idiom in other functional languages. I was struck by two things you said: you have to know cons'ing to understand x::xs and each symbol, x and xs should be unambiguously named in isolation (words to that effect).
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.
- klibertp 14y ago"What I see in "x::xs" is one simple expression at a glance." That's exactly what I wrote about in this: "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." The thing here is that I don't believe that you would treat 'head :: tail' any different than 'x :: xs'. I believe that you would still see 'head :: tail' as one simple expression: I certainly would. If so then there is really no reason to avoid using 'head' and 'tail' as names. "longer symbols surely can't help" Do you know lisp? If yes, skip this; if not, read on. I'll give you two example snippets of code in Racket. Both use exactly the same syntax and mean the same; read the first one and try to answer the question what the code does. The read the second one and try it again. (define (drt t) (define-values (p l) (values 0 (s:l t))) (λ (dc x y a) (drc dc x y (s:s (s:r t p)) a) (set! p (add1 p)) (when (>= p l) (set! p 0)))) Assume that `s:l`, `s:s` and `s:r` are provided from another module that you have only in a compiled form at the moment and that drc is defined in the same module but written in the same style. Can you tell what is happening here? The second snippet looks like this: (define (draw-text text-string) (define-values (current-pos text-len) (values 0 (string-length text-string))) (lambda (drawing-context coord-x coord-y angle) (define current-letter (string (string-get-char-at text-string current-pos))) (draw-char drawing-context coord-x coord-y current-letter angle) (set! current-pos (add1 current-pos)) (when (>= current-pos text-len) (set! current-pos 0)))) Are you able to guess what this does now? Are you able to guess even a bit more than from the first snippet?
- papsosouid 14y ago>Assume that `s:l`, `s:s` and `s:r` are provided from another module that you have only in a compiled form at the moment Why are you being so deliberately dishonest, and expecting a response like "oh, your strawman has totally convinced me to write shitty code, thank you so much first year CS student!"? The entire point is that x and xs are not defined elsewhere, they are defined by you, and used on a single line.
- klibertp 14y ago"Why are you being so deliberately dishonest" I'm not - that's how they are defined in Racket. "and expecting a response like" Why do you assume so? I'm really curious what is the difference in the readability to someone who doesn't know the syntax. "first year CS student" Ok, so I had enough: you're just a stupid troll. Not only you can't back your claims with arguments but now you're (for the third time, no less) using ad hominem "argument"; it's useless talking to you since you're too stupid to formulate sensible response and you're too bad a programmer to even have a chance of knowing something interesting. "and used on a single line" Oh, on a single line? That's fine then...
- papsosouid 14y ago>I'm not - that's how they are defined in Racket. You are. The argument is that you should be using single letter variables for small scope, simple variables. Like the classics: for (i = 0; i < whatever; i++) { myarray[i] = something(); } and myfunction x:xs = dothing x + myfunction xs When you decide to deliberately misrepresent that argument as "use single letter variables across modules" that is dishonest. >Oh, on a single line? That's fine then... Yes, of course it is.
- klibertp 14y agoI'm still waiting for your argument as to why is one-letter name better than three-letters one. What is the difference, what makes it so much harder to understand the name with two additional letters? With a link to the source please.