Debugging in uLisp

If your uLisp program is refusing to run, or is not doing what you wanted or expected it to do, what should you do next? Here are some suggestions for debugging uLisp programs.

Break your program down into smaller functions

Debugging an application is much easier if you break it down into small parts that you can test individually. For example, if you have a program that performs four actions in sequence, write each action as a separate function so you can test it on its own before integrating it into the whole program.

For an example of this approach see the examples, such as Simon game.

Insert print statements

The print function takes an argument, prints its value, and returns the same value, so you can wrap a print statement around most parts of a uLisp program to see what's happening without affecting its operation.

For example, here's a program which is intended to sum the numbers 1 to 4:

(let ((sum 0)) (dotimes (x 4) (incf sum x)) sum)

When we run it we get the answer 6 rather than 10 as expected. What's going on? One approach is to put print around the x as follows:

(let ((sum 0)) (dotimes (x 4) (incf sum (print x))) sum)

Now when we run it the value of x gets printed each time around the loop:

> (let ((sum 0)) (dotimes (x 4) (incf sum (print x))) sum)

3 6
The problem is revealed: the dotimes loop counts from 0 to 3 rather than 1 to 4.

Insert a break statement

The break statement interrupts your program and jumps to a prompt where you can examine the values of the local variables, or even change them. You can then type nil to continue, executing the rest of your program from where was interrupted.

For example, here's a program intended to set the brightness of an LED for values of x from 1 to 16 by calculating y = x2 - 1 and setting the analogue output to y:

(defun set (x) (let ((x (* x x)) (y (- x 1))) (analogwrite 9 y)))

Unfortunately the LED is always dim, even when we execute (set 16). What's going on? The solution is to insert a call to break:

(defun set (x) (let ((x (* x x)) (y (- x 1))) (break) (analogwrite 9 y)))

Now when we run it we can examine the variables when the program is interrupted with the break:

> (set 16)

: 1> x

: 1> y

The problem is revealed: y is taking the value of x from the parameter, not the previous let assignment. What we need is let*:

(defun set (x) (let* ((x (* x x)) (y (- x 1))) (analogwrite 9 y))) 
Now the program works as intended!

Contact us

If all else fails, post your problem on the uLisp Forum so that the community can help you.

If you suspect that your problems are due to a bug in uLisp please email .