vancan1ty
vancan1ty

Reputation: 563

Common lisp: break, but return input instead of nil?

I am new to lisp, and am learning as I go.

The standard common lisp break function 1. pops you into the debugger, and 2. if you choose to continue, returns nil.

It seems to me that a break function that popped you into the debugger but RETURNED ITS INPUT would be extremely useful. Then, you could just insert it transparently around a given s-expression to look at the state of the program at that point.

So I could do something like

CL-USER> (break-transparent (+ 1 2))

which would pop me into the debugger and let me look around and then would return

3

Is there such a thing in lisp, and, if not, is there a way to make such a thing? I am not good with macros yet.

Thanks,

EDIT: Doug Currie kindly answered this below with a simple macro. Here is my slightly modified version for anyone else with this question that displays the argument to break-transparent front and center in the debugger window.

(defmacro break-transparent (exp)
  `(let ((x ,exp)) (break "argument to break: ~:S" x) x))

Upvotes: 0

Views: 1021

Answers (2)

Rörd
Rörd

Reputation: 6681

Since you've added the macro from the other answer to your question in a slightly modified version, here's a fixed version of that that doesn't add a new identifer that might interfere with an existing one:

(defmacro break-transparent (value)
  (let ((g (gensym)))
    `(let ((,g ,value))
      (break "argument to break: ~:S" ,g)
      ,g)))

Upvotes: 0

Doug Currie
Doug Currie

Reputation: 41180

You can write break-transparent as a macro that expands to:

(progn
  (break)
  (+ 1 2))

or if you really want to evaluate the expression before the break:

(let ((x (+ 1 2)))
  (break)
  x)

So,

(defmacro (break-transparent exp)
  `(let ((x ,exp)) (break) x))

Upvotes: 2

Related Questions