6

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 ;;?

3 Answers 3

7

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).

Sign up to request clarification or add additional context in comments.

Comments

6

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.

1 Comment

Note that most schemes don't have "doc-strings".
5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.