FizzBuzz in Common Lisp
1. Divisible function

It is perfectly fine to define a function like divisible to improve the reading of a program. In general, when defining a function that could be reused, it is a good practice to comment its meaning (and this is encouraged by the syntax of the language):

(defun divisible (a b)
  "a is evenly divisible by b"
  (zerop (mod a b)))


Note the idiomatic use of the predicate (zerop x) instead of (= 0 x).

2. Introduction of variables

The use of setf (or setq) to introduce new variables is wrong (you should have received a warning, something like In FIZZBUZZ: Undeclared free variable OUT). The setf and setq operators should be used only to modify them.

Instead, you can introduce local variables with the let syntax:

(let ((new-local-variable1 initial-value)
      (new-local-variable2 initial-value)
      ...)
  body-in-which-the-new-local-variables-are-bound)


So fizzbuzz could be redefined as:

(defun fizzbuzz (n)
  "return n or Fizz or Buzz or FizzBuzz"
  (let ((out (string "")))

    (unless (or (divisible n 3) (divisible n 5))
      (return-from fizzbuzz n))

    (when (divisible n 3)
      (setf out (format nil "~d~d" out "Fizz")))

    (when (divisible n 5)
      (setf out (format nil "~d~d" out "Buzz")))

  out))


The global variables are instead introduced by defvar, so you could write for instance:

(defvar 1to100 (range 1 100))
(defvar 1to100fb (map 'list #'fizzbuzz 1to100))


3. The algorithm

The function range actually builds a list of (max-min+1) elements, filled with the values min, min+1, min+2, ..., max. The use of map over 1to100 is quite an expensive way of performing a simple iteration from the integer min to the integer max! Others, much more efficient ways, exist. For instance, you could do a classical loop (think of a for in a C-like language):

(loop for i from 1 to 100
  do (format t "~d~%" (fizzbuzz i)))


Another possibility is to use the dotimes operator:

(dotimes (i 100)
   (format t "~d~%" (fizzbuzz (1+ i))))


that iterates from 0 to (but not included) 100 (so there is the need of use the 1+ function).

If, on the other hand, you want to go for recursion, here is a possible solution (which is tail-recursive, so compiled by most systems as an iterative loop):

(defun fizzbuzz-to (limit &amp;optional (start 1))
  (when (&lt;= start limit)
    (format t "~d~%" (fizzbuzz start))
    (fizzbuzz-to limit (1+ start))))


4. Types

In a format string, for a general printing ~a should be used instead of ~d, which mainly is used to print integer (decimal) numbers. It can be used also for strings, but this is due to the fact that in the specification it is said that:


  if arg is not an integer, it is printed in ~A format and decimal base.


What I do not like more, however, is the fact that the function fizzbuzz sometimes returns an integer, sometimes a string. The fact that Common Lisp has dynamic typing in some way hides this fact, but I think that the program would be more elegant (and in some sense more correct) if the result has always the same type.

One way is to redefine the function in this way:

(defun fizzbuzz (n)
  (cond ((divisible n 3)
         (if (divisible n 5)
             "FizzBuzz"
             "Fizz"))
        ((divisible n 5) "Buzz")
        (t (format nil "~d" n))))


For other solutions, you could for instance look at this CR question.