(defun test (n)
      (let ((list1 ()))
        (while (< (length list1) 10))
        (push (random n) list1)

I'm trying to add (random n) to list1 while the length of list1 is less than 10. When I evaluate this function, it hangs. What am I doing wrong?

1 Answer 1


You closed your while loop before doing the push. So the list length stayed less than 10.

(defun test (n)
  (let ((list1 ()))
    (while (< (length list1) 10)
      (push (random n) list1))

Moral: Indent your code properly. C-M-q is your friend, as is TAB. See the Emacs manual, node Indenting Several Lines.

Hanging should have been your clue that the while loop was infinite. That would tell you that the list length wasn't increasing.

  • Great, thanks! I didn't know about C-M-q I was trying fo find a package to do proper indentation.
    – zeynel
    Oct 16, 2021 at 19:59
  • You're welcome. C-h m is your friend. In emacs-lisp-mode it mentions C-M-q (though admittedly it doesn't say much about it). You can look up its command (indent-pp-sexp in the Emacs manual, using C-h S). That takes you here.
    – Drew
    Oct 16, 2021 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.