2

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". ?

David542
  • 104,438
  • 178
  • 489
  • 842
  • 1
    Get a good IDE that understands Lisp syntax. I have heard that Eclipse is a good one for this. – Robert Harvey Mar 27 '21 at 01:17
  • https://stackoverflow.com/questions/2207255/lisp-parentheses might help – ggorlen Mar 27 '21 at 01:18
  • 3
    Racket comes with its own IDE that manages parentheses. It seems that most who program regularly with Common Lisp use Emacs with [Slime](https://common-lisp.net/project/slime/) or [Sly](https://github.com/joaotavora/sly), and with Scheme [Geiser](https://github.com/emacsmirror/geiser). There is also [racket-mode](https://racket-mode.com/) with Emacs for Racket. [paredit](https://www.emacswiki.org/emacs/ParEdit) is often combined with these in Emacs. There are similar plugins for Vim, but I use Emacs for lisps, so I am less familiar with those. – ad absurdum Mar 27 '21 at 05:49
  • The text editor Kate that comes bundled with a lot of Linux distros has syntax highlighting, understands Scheme syntax, and colors matching parentheses rainbow-style to make them more visible; all of this out-of-the-box. – ad absurdum Mar 27 '21 at 07:19
  • @adabsurdum do you know if VS Code is popular for any list-like syntaxes? – David542 Mar 27 '21 at 18:38
  • I haven't used VS Code; I'm sure that some people use it, but it would probably be a stretch to say that it is popular. But, for beginning experiments with lisps you should use whatever you are comfortable with, so long as it works for you. If you find yourself spending a lot of time with lisps, you might decide on something else for an editor/IDE, but then you will be better equipped to make choices. – ad absurdum Mar 27 '21 at 19:29
  • Lot of editors have parentheses matching feature, but you need to find it in configuration and enable it. I use GNU/Linux and even gedit and mousepad (Simple editors for XFCe and Gnome) have this function. On windows you can use simple Notepad++. And probably every modern IDE support balancing of parentheses, this is common functionality. – jcubic Apr 18 '21 at 19:39

2 Answers2

1

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?

Will Ness
  • 70,110
  • 9
  • 98
  • 181
  • I don't like rainbow parenths either, but it is handy for matching pairs to be indicated by blinking or changing color when you are traversing them, e.g., when you are looking for the right place to insert an expression. – ad absurdum Apr 01 '21 at 12:38
  • 1
    @adabsurdum that, yes. also the ability to jump to the matching parenthesis. – Will Ness Apr 01 '21 at 12:41
0

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.

haziz
  • 12,994
  • 16
  • 54
  • 75