Reputation: 110582
I'm playing around with writing some code in scheme. Here's an example of doing a fibonacci:
(define (fib n)
; if n == 0:
(if (= n 0)
; return 0
0
; else if n==1
(if (= n 1)
; return 1
1
; else return fib(n-1) + fib(n-2)
(+ (fib (- n 1)) (fib (- n 2)))))
)
(fib 4)
(fib 3)
(fib 2)
(fib 1)
(fib 0)
3
2
1
1
0
My question is more about general advice. How in the world do you keep track of all the parentheses? It's not so much a matter of legibility (I don't care about that) but more just a matter of getting things right and not having to do trial-and-error in ten difference places to add in extra parens to see if things work when it's all said and done.
Is there a good way to get a better handle on the parens, such as a good IDE or web environment or what-not, or is the answer just "get used to it". ?
Upvotes: 2
Views: 158
Reputation: 13612
Use either Emacs with Geiser, or alternatively DrRacket.
DrRacket will let you set the language to R5RS (or R7RS) if you wish to use standard Scheme rather than Racket, which is a close relative.
Upvotes: 0
Reputation: 71119
The usual non-IDE Lisp answer to this is indentation.
You let it guide you in reading the code. You take it on faith that the parens are balanced to follow the indentation structure of the code that you see.
While writing, you usually keep mental track of what's going on locally, and when you close a paren, you know which expression is it ending.
Keep these expressions short, start each new one on the new line under the previous one of the same semantic level, indented to the same level of indentation (the rainbow coloring for me personally is not helpful in the slightest, more like distracting and confusing).
As an example, your code properly formatted under these guidelines, becomes
(define (fib n)
(if (= n 0)
0
(if (= n 1)
1
(+ (fib (- n 1))
(fib (- n 2)))))
which we can read without paying the slightest attention to the parentheses.
And legibility is the whole point to writing the code in the first place, is it not?
Upvotes: 1