Search code examples
schemelispsicp

Is there a style guide for SICP exercises?


I'm currently working my way through SICP, but I'm not quite used to the style of writing Scheme code yet. Is there any sort of style guide that is a companion for the book? So far, I have only found the comments on "pretty-printing" that are found in section 1.1.1.


Solution

  • Gerald Jay Sussman, one of the authors of SICP, is also one of the authors of Scheme. Their really cool 1986 video lecture at HP they don't expect Scheme to be that well known so they call it the more general name Lisp. Don't get confused since SICP is 100% Scheme and thus scheme coding style would be the correct path.

    The Scheme wiki has a style guide together with common variable naming conventions and comment style.

    Scheme was a new dialect of Lisp with lexical closures and one namespace as core features. It uses define instead of defun, defparameter, and defvar. DrRacket IDE actually treats lists with the operator starting with "de" as define. eg.

    ;;; example procedure test
    (define (test arg1 arg2)
      ;; two space indent after define, let and friends
      (if (test? arg1 arg2)                   ; predicates tend to end with ?
          (consequent arg1 arg2)              ; if you split if then arguments align
          (alternative "extra long argument"  ; if you split arguments in procedure call arguments are aligned
                       arg1
                       arg2)))                ; ending parens keep together
    

    In Common Lisp most of the coding style is the same:

    ;;; example function test
    (defun test (arg1 arg2)
      ;; two space indent after defun, let and friends
      (if (testp arg1 arg2)                   ; predicates tend to end with p
          (consequent arg1 arg2)              ; if you split if then arguments align
          (alternative "extra long argument"  ; if you split arguments in procedure call arguments are aligned
                       arg1
                       arg2)))                ; ending parens keep together
    

    The standard reference for Common Lisp style, including commenting conventions, is Peter Norvig and Kent Pitman's Tutorial on Good Lisp Programming Style. This you can use as a supplement for the Scheme resources.

    PS: Coding style is opinionated. The language don't care much for any of this so this is just to make the code easier to read for humans.