Winston wrote: ↑Tue Oct 13, 2020 12:36 pm
...which is better, "print" or "throw 'err"?
It depends what you want to do.
I'll write about PRINT here, then another post about THROW.
PRINT writes a string (text) to the default output stream - For Nyquist in Audacity the default output stream is the debug window. It also returns the string. Thus if a PRINT statement is the final line run, then the string will be returned to Audacity and Audacity will display it in a message window.
See:
https://www.audacity-forum.de/download/ ... ef-200.htm
The PRINT statement can be useful when debugging code. For example this code fails:
Code: Select all
(setf data (list "A" "B" "C" "D"))
(setf myarray (make-array 4))
(setf count 1)
(dolist (val data)
(setf (aref myarray count) val)
(setf count (+ count 1)))
(print myarray)
The debug output is not particularly easy to understand
Code: Select all
error: index out of range - 4
Function: #<FSubr-SETF: #55555868d418>
Arguments:
(AREF MYARRAY COUNT)
VAL
Function: #<FSubr-DOLIST: #555558c6e088>
Arguments:
(VAL DATA)
(SETF (AREF MYARRAY COUNT) VAL)
(SETF COUNT (+ COUNT 1))
... (more)...
To see what is happening within the DOLIST loop, we can add
=https://www.audacity-forum.de/download ... 9.htmPRINC and PRINT statements like this:
Code: Select all
(setf data (list "A" "B" "C" "D"))
(setf myarray (make-array 4))
(setf count 1)
(dolist (val data)
; print some stuff to help debug
(princ "Count is: ")
(princ count)
(princ " and val is: ")
(print val)
; end of print statements
(setf (aref myarray count) val)
(setf count (+ count 1)))
(print myarray)
The debug output is then:
Code: Select all
Count is: 1 and val is: "A"
Count is: 2 and val is: "B"
Count is: 3 and val is: "C"
Count is: 4 and val is: "D"
error: index out of range - 4
Function: #<FSubr-SETF: #555558681858>
Arguments:
(AREF MYARRAY COUNT)
VAL
Function: #<FSubr-DOLIST: #555558687e78>
So we can see that the list is being stepped through correctly, but our "count" variable is wrong because
=https://www.audacity-forum.de/download ... rt at zero.
We can then fix our code like this:
Code: Select all
(setf data (list "A" "B" "C" "D"))
(setf myarray (make-array 4))
(setf count 0)
(dolist (val data)
; print some stuff to help debug
(princ "Count is: ")
(princ count)
(princ " and val is: ")
(print val)
; end of print statements
(setf (aref myarray count) val)
(setf count (+ count 1)))
(print myarray)
and see that it now works as expected.
After getting it working correctly, the diagnostic PRINT statements should be removed:
Code: Select all
(setf data (list "A" "B" "C" "D"))
(setf myarray (make-array 4))
(setf count 0)
(dolist (val data)
(setf (aref myarray count) val)
(setf count (+ count 1)))
(print myarray)