Reputation: 29
I have this function that convert a number into a certain base.I want to report an error if the base I introduce is greater than 9 and smaller than 2 but I dont know. Can you help me please?
(defun zecb (number base)
(write-to-string number :base base))
Upvotes: 0
Views: 98
Reputation: 949
If what you want is printing an error, you can use one of the write/print function available (see Output to character streams).
Example:
(defun zecb (nb base)
"Test"
(if (or (< nb 2) (> nb 9))
(princ "Number Not in range")
(* nb base)))
Upvotes: 0
Reputation: 139321
CL-USER 99 > (defun test (n)
(check-type n (integer 2 9))
n)
TEST
CL-USER 100 > (test 2)
2
CL-USER 101 > (test 9)
9
CL-USER 102 > (test 10)
Error: The value 10 of N is not of type (INTEGER 2 9).
Upvotes: 5
Reputation: 16156
Based on the condition system that coredump already mentioned there's also an assert
macro that is part of Common Lisp. Try the following in your REPL:
(defun divide (num denom)
(assert (not (= 0 denom))
(denom)
"Attempted to divide ~a by zero, use a different denominator!" num)
(/ num denom))
(divide 42 0)
Upvotes: 4
Reputation: 3452
What do you mean by "report error"? If you mean something like throwing an exception in another languages, error
may sute your needs:
(error "Base must be between 2 and 9")
Read more about common lisp conditions and handlers, if you need something more sufisticated than simple-error.
Upvotes: 2