This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
004optional_parens
75 lines (65 loc) · 1.83 KB
/
004optional_parens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
--- Optional parens
Wart is indentation sensitive. Multi-word lines without leading parens are
implicitly grouped with later indented lines:
if (n > 0)
n * (n - 1)
=>
(if (n > 0)
(n * (n - 1)))
No indented lines after? They're grouped in isolation:
a b
c d
=>
(a b)
(c d)
Lines with a single word are never wrapped in parens:
def (foo)
x
=>
(def (foo)
x) # x is returned as a value, never called
Lines with a leading paren are never wrapped in parens:
def (foo x)
(prn x) x
=>
(def (foo x)
(prn x) x)
Putting these rules together, parens are not required around 'if' in:
if (x%2 = 1)
'odd
'even
..but they are required in:
(if # parens required because line has a single word
(x%2 = 1)
'odd
:else # optional, sometimes more clear
'even)
..and, furthermore, this is wrong:
if (x%2 = 1)
'odd
:else
'even
=>
(if (x%2 = 1)
'odd)
:else
'even # wrong
Indentation-sensitivity is disabled inside parens. This rule is useful if you
want multiple expressions on a single line:
(if test1 body1 # outer parens required to avoid grouping test2 and body2
test2 body2
else-expr)
It also implies that backquoted expressions must be fully parenthesized:
mac (when cond ... body)
`(if ,cond
(do ,@body)) # parens before 'do' are required
This helps make explicit the syntax trees being manipulated.
--- "But I hate significant whitespace!"
I'm not trying to minimize parens typed; I'm trying to make Lisp code more
readable to non-lispers. Wart's codebase tastefully removes parens from just
control-flow operators (def/mac/if/while/..), leaving them alone everywhere
else. When in doubt, I insert parens.
If you don't like this approach, just use parens everywhere:
(def (foo) 34)
(def (foo)
34)