Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scheme Coding Style Questions

I am confused about the Scheme style for my code.

Should I format if forms as:

a.

if()
  ()
  ()

or b.

  if () ()
        ()

or c.

if () () ()

Should I format cond clauses as
a.

  cond ()
       ()

or b.

cond
()
()

When do I use a single ; to comment and a double ;;?

like image 703
unj2 Avatar asked Jul 05 '09 15:07

unj2


3 Answers

Here is a Lisp style guide, and here is a recommended commenting style.

If you have an emacs style editor, typing C-M-q within your s-expression should format it for you; it will get you correctly formatted code if your line breaks are reasonable (and editor configuration for indent-alist hasn't been munged too badly).

like image 65
Doug Currie Avatar answered Nov 16 '22 12:11

Doug Currie


To fill in Doug's answer for your specific questions:

(if test
    then
    else)

(cond
  (test1 exp1)
  (test2 exp2)
  (else exp3))

Or, for conds with long series of expressions:

(cond
  (test1
   exp1
   exp2)
  (else
   exp3
   exp4))

Comment conventions are a little looser. When I am writing careful code, I do something like this:

;;; new section ;;;
;;; section comments


(define (f g . x)
  "docstring goes here"
  ;; in-function comments
  (g x)) ; trailing line comment

But the exact boundaries for ; vs ;; usage vary. In particular, some people (including me) do not much like trailing line comments and will instead use ; for in-function comments and ;;; for section comments.

like image 34
Nathan Shively-Sanders Avatar answered Nov 16 '22 12:11

Nathan Shively-Sanders


Have a look at Peter Norvig's "Tutorial on Good Lisp Programming Style" though you would have found the answer to your particular question in any Scheme/Lisp book.

like image 5
Luís Oliveira Avatar answered Nov 16 '22 10:11

Luís Oliveira