by lihaoyi · 1y ago · 0 comments
(define (match:element variable restrictions)
(define (ok? datum)
(every
(lambda (restriction)
(restriction datum)
)
restrictions
)
)
(define (element-match data dictionary succeed)
(and
(ok? data)
(let ((vcell (match:lookup variable dictionary)))
(if vcell
(and
(equal? (match:value vcell) data)
(succeed dictionary)
)
(succeed (match:bind variable data dictionary))
)
)
)
)
element-match
)
It may not be densest or most compact indentation scheme, but damn is it readable for someone without a lisp/scheme background!Likewise, a lispier(?) and more compact compromise?:
(define (match:element variable restrictions)
(define (ok? datum)
(every
(lambda (restriction)
(restriction datum))
restrictions))
(define (element-match data dictionary succeed)
(and
(ok? data)
(let ((vcell (match:lookup variable dictionary)))
(if vcell
(and
(equal? (match:value vcell) data)
(succeed dictionary))
(succeed (match:bind variable data dictionary))))))
element-match)Counting parens is silly when the editor can do it for you.
About 90% of my problems arise when I shift the parens, e.g. going from `(+ (1 2))` to `(+ (+ 1 2) 3)` or the other way round - and if there is now ) or )) at the very end (but more likely at 4+, not 1 or 2.
How would any plugin know what I mean here? (I know + is a bad example as its commutative...).
The reading is not that bad as the indentation tells you where the brackets are and you don't really notice them.
This looks like how I've naively tried to format things, and having started with python kind of makes sense.
But trying to read / edit code with 5 parentheses bunched together, I have the hardest time telling if they are matched or where. Thank goodness for vim / helix auto-matching.
I just use the Tab key for indenting and it always work for me (there's a logic on where to break line to get correct indenting, and it makes it easy to see if you misplaced a parenthesis. Also, both Emacs and Vim, highlight the matched parenthesis, so I never wonder about if I have the correct number of closing parenthesis.
(foo
(bar
(baz (xyzzy arg))
(bar arg))
We know from the indentation that this is supposed to be a closed unit: (bar
(baz (xyzzy arg))
But it needs three closing parens ))) not just )).If this problem is not accidentally compensated by a ) in the wrong place elsewhere, this code will error out.
One simple thing you can do is just automatically reindent the whole block. The indentation will then match the incorrect parentheses and the problem will be clear. Undo the temporary reformatting and fix it.
This is exactly the point of my comment -- the absence of an obvious / reasonable automatic formatter is the problem I'm pointing to.
Honestly though, this style for ending parenthesis of block-type nodes makes a lot of sense to me. And you still retain the benefit of macro-ing your own syntax when you need it.
(
define (match:element variable restrictions)
(
define (ok? datum)
(
every
(
lambda (restriction)
(restriction datum)
)
restrictions
)
)
(
define (element-match data dictionary succeed)
(
and
(ok? data)
(
let ((vcell (match:lookup variable dictionary)))
(
if vcell
(
and
(equal? (match:value vcell) data)
(succeed dictionary)
)
(succeed (match:bind variable data dictionary))
)
)
)
)
element-match
)Granted I'm not the one to optimize for since I don't use Lisp, but this just seems sane and easy to automate, and it looks like other non-Lisp languages.
Should we be doing this?:
void MyFunc() {
DoStuff();
DoOtherStuff();}I'll happily tolerate some wasted space in that trade-off.
My "OTBS all the things" rules:
- Never horizontal align. Indentation is a simple tree, with each set of children indented 1 layer deeper, which can be 4 spaces or 1 tab.
ThisKindOfThing(
foo,
bar,
baz);
is generally dumb.- If a block is worth indenting for, the end of the block deserves its own line. Just do OTBS but also for parentheses if you've a long function call.
ThisKindOfThing (
foo,
bar,
baz
);
Is just more consistent and easier to manage while still being legible.- if you're ever splitting something across multiple lines there must be an indent.
myObj
.Foo()
.Bar()
.Baz();
- you're allowed to double-dip on tree depth - that is indent only one level for like 3 levels of tree, as long as the opening of that 3-deep jump is a clear one-liner and the end of it is a line that says ")))". Foo(Bar(Baz(
someVal
)));
- when splitting lines on infix operators, anything but commas go at the start of the line (also, bedmas counts as tree depth) foo
+ bar
+ baz
+ (
var1
* var2
* var3
)
+ quux;(I hope?!)