schemelisp

what is the proper way to evaluate Lisp/Scheme in emacs


I try this LISP snippet from the Structure and Interpretation book

(define (a-plus-abs-b a b)
  ((if (> b 0) + -) a b))

in emacs by C-x C-e, or by evaluating the whole buffer. either way and get the backtrace:

Debugger entered--Lisp error: (void-function define)
  (define (a-plus-abs-b a b) ((if (> b 0) + -) a b))
  (progn (define (a-plus-abs-b a b) ((if (> b 0) + -) a b)))
  eval((progn (define (a-plus-abs-b a b) ((if (> b 0) + -) a b))) t)
  elisp--eval-last-sexp(nil)
  eval-last-sexp(nil)
  funcall-interactively(eval-last-sexp nil)
  call-interactively(eval-last-sexp nil nil)
  command-execute(eval-last-sexp)

If I try {evaluate-defun} I get a different error: symbol's definition is void: define

what is the proper way to evaluate the snippet? should I be using sth outside emacs?


Solution

  • Either way of executing Emacs Lisp works.

    What the issue here is that SICP uses a Lisp called Scheme. This is a language that differs from Emacs Lisp. The main difference is that Scheme is a Lisp-1 and so a variable and a function can't have the same name and so define is used to create both. Whilst Emacs Lisp is a Lisp-2 and you can have a function and a variable with the same name, so defun to define a function and defvar for a variable - or usually setq to set a variables value and also define it if required.

    If you want to try SICP you need to get a Scheme interpreter or possibly Racket and use its Scheme language mode.