Spent several hours today trying to figure out why my #elisp code was misbehaving. Turns out, I made a mistake while trying to be clever. Instead of doing:

(when (and cond1 cond2)
  stuff
  ...)

I did:
(and cond1 cond2
  stuff
  ...)

...which would only work if every instruction in stuff returned a non-nil value.

I should stop trying to be clever.

#emacs

Edit: when, not while

Dave Marquardt reshared this.

in reply to Jonathan Lamothe

For the morbidly curious, this is the (now corrected) code:
(defun jrl-collect-data (table input output)
  "Collect table data into lists of outputs per input"
  (let (res record in-val out-val collection)
    (while table
      (setq record (car table)
            in-val (assoc input record)
            out-val (assoc output record))
      (when (and in-val out-val)
        (pop in-val)
        (pop out-val)
        (setq collection (assoc in-val res))
        (if collection
            (push out-val (cdr collection))
          (push (list in-val out-val) res)))
      (pop table))
    res))

I should probably throw some comments in here, but essentially the assoc in the when block always returns nil on the first iteration of the loop, and if it can't progress past that point, it'll always return nil on subsequent loops and nothing will ever happen. As a result, any list I might feed into this function is always going to result in a nil output.

This website uses cookies. If you continue browsing this website, you agree to the usage of cookies.