8 ms·
Does one actually need whitespace to separate identifiers? This is something that's always bugged me a bit. Aside from C-style type declarations ("unsigned int
by m48 8y ago
Does one actually need whitespace to separate identifiers? This is something that's always bugged me a bit.
Aside from C-style type declarations ("unsigned int x;"), C-style syntaxes seem to always have ways other than whitespace to separate identifiers.
Like (using some JavaScript in a hypothetical example) I can't think of many concrete reasons why this is easier to parse:
let first_number=2, second_number=2, answer=first_number-second_number;
...than this:
let first number=2, second number=2, answer=first number-second number;
Although, of course, some languages—most Lisps, Tcl, and Red/REBOL come to mind—actually do rely on whitespace and whitespace alone to separate identifiers in many situations, and something like this would likely be unworkable there.
- mkl 8y agoTikZ allows whitespace in identifiers. At first it was pretty strange, but I actually really like it now. I don't think parsing it is much of a problem, and I would quite like to be able to use it in other languages.
- m48 8y agoWell, thinking about it more, I did realize there's a pretty nasty edge case with my hypothetical JavaScript syntax: let let x = 5; let x = 6; // should this set the variable "let x"? // or define a variable named "x"? One could potentially design around situations like this, but allowing whitespace in identifiers likely does require being much more meticulous about treatment of reserved words, identifiers, and whitespace than more traditional syntaxes, and this is likely why not many people attempt this. I think the idea is worth experimenting with, though, and that a good implementation of it could be convenient enough for end-users to outweigh the implementation inconvenience.
- lispm 8y agoThough one can use whitespace in Common Lisp identifiers, by quoting symbols: CL-USER 115 > (let ((first| |number 10) (second\ number 20)) (+ first\ number |SECOND NUMBER|)) 30
- m48 8y agoOh, I never noticed that. That's pretty interesting—although unfortunately, that syntax does not look terribly convenient to write, which is the main thing I'm after here. I think the best way to get identifiers with whitespace to work in a Lisp would be contrive a syntax for S-expressions that uses something other than whitespace to separate things. Perhaps letting (first rest-1 rest-2 ...) be written as as (first: rest 1, rest 2, ...) or (first, rest 1, rest 2, ...), so that example could be written as: (let: ((first number: 10), (second number: 20)), (+: first number, second number)) I imagine it would be possible to write a macro in Common Lisp to transform this into runnable code, or a language in Racket to do so—although, I'm not sure how many people would actually want to make or use something like this.
- kazinator 8y agoNot all whitespace you see in Lisp code is strictly necessary: TXR Lisp: 1> (list 1"a"'(b(c)d(e))) (1 "a" (b (c) d (e))) Here we just have one space that prevents list 1 from being list1.