common-lispclisp

What exactly does the #. (sharpsign dot) do in Common Lisp? Is it causing a variable has no value error?


Edit: Title updated to reflect what my question should have been, and hopefully lead other users here when they have the same problem.

Little bit of a mess, but this is a work-in-progress common lisp implementation of anydice that should output some ascii art representing a probability density function for a hash-table representing dice rolls. I've been trying to figure out exactly why, but I keep getting the error *** - SYSTEM::READ-EVAL-READER: variable BAR-CHARS has no value when attempting to run the file in clisp. The error is originating from the output function.

The code is messy and convoluted (but was previously working if the inner most loop of output is replaced with something simpler), but this specific error does not make sense to me. Am I not allowed to access the outer let* variables/bindings/whatever from the inner most loop/cond? Even when I substitute bar-chars for the list form directly, I get another error that char-decimal has no value either. I'm sure there's something about the loop macro interacting with the cond macro I'm missing, or the difference between setf, let*, multiple-value-bind, etc. But I've been trying to debug this specific problem for hours with no luck.

(defun sides-to-sequence (sides)
  (check-type sides integer)
  (loop for n from 1 below (1+ sides) by 1 collect n))

(defun sequence-to-distribution (sequence)
  (check-type sequence list)
  (setf distribution (make-hash-table))
  (loop for x in sequence
    do (setf (gethash x distribution) (1+ (gethash x distribution 0))))
  distribution)

(defun distribution-to-sequence (distribution)
  (check-type distribution hash-table)
  (loop for key being each hash-key of distribution
    using (hash-value value) nconc (loop repeat value collect key)))

(defun combinations (&rest lists)
  (if (endp lists)
      (list nil)
      (mapcan (lambda (inner-val)
                (mapcar (lambda (outer-val)
                          (cons outer-val
                                inner-val))
                        (car lists)))
              (apply #'combinations (cdr lists)))))

(defun mapcar* (func lists) (mapcar (lambda (args) (apply func args)) lists))

(defun dice (left right)
  (setf diceprobhash (make-hash-table))
  (cond ((integerp right)
         (setf right-distribution
               (sequence-to-distribution (sides-to-sequence right))))
        ((listp right)
         (setf right-distribution (sequence-to-distribution right)))
        ((typep right 'hash-table) (setf right-distribution right))
        (t (error (make-condition 'type-error :datum right
                                  :expected-type
                                  (list 'integer 'list 'hash-table)))))
  (cond ((integerp left)
         (sequence-to-distribution
          (mapcar* #'+
                   (apply 'combinations
                     (loop repeat left collect
                       (distribution-to-sequence right-distribution))))))
        (t (error (make-condition 'type-error :datum left
                                  :expected-type
                                  (list 'integer))))))

(defmacro d (arg1 &optional arg2)
  `(dice ,@(if (null arg2) (list 1 arg1) (list arg1 arg2))))

(defun distribution-to-probability (distribution)
  (setf probability-distribution (make-hash-table))
  (setf total-outcome-count
        (loop for value being the hash-values of distribution sum value))
  (loop for key being each hash-key of distribution using (hash-value value)
    do (setf (gethash key probability-distribution)
             (float (/ (gethash key distribution) total-outcome-count))))
  probability-distribution)

(defun output (distribution)
  (check-type distribution hash-table)
  (format t "   #  %~%")
  (let* ((bar-chars (list 9617 9615 9614 9613 9612 9611 9610 9609 9608))
    (bar-width 100)
    (bar-width-eighths (* bar-width 8))
    (probability-distribution (distribution-to-probability distribution)))
    (loop for key being each hash-key of
      probability-distribution using (hash-value value)
      do (format t "~4d ~5,2f ~{~a~}~%" key (* 100 value)
                 (loop for i from 0 below bar-width
                       do (setf (values char-column char-decimal)
                                (truncate (* value bar-width)))
                       collect
                       (cond ((< i char-column)
                              #.(code-char (car (last bar-chars))))
                             ((> i char-column)
                              #.(code-char (first bar-chars)))
                             (t
                              #.(code-char (nth (truncate
                                                 (* 8 (- 1 char-decimal)))
                                                bar-chars)))))))))

(output (d 2 (d 2 6)))

This is my first common lisp program I've hacked together, so I don't really want any criticism about formatting/style/performance/design/etc as I know it could all be better. Just curious what little detail I'm missing in the output function that is causing errors. And felt it necessary to include the whole file for debugging purposes.


Solution

  • loops scoping is perfectly conventional. But as jkiiski says, #. causes the following form to be evaluated at read time: bar-chars is not bound then.

    Your code is sufficiently confusing that I can't work out whether there's any purpose to read-time evaluation like this. But almost certainly there is not: the uses for it are fairly rare.