help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cl-dolist, dolist, cl-return,


From: John Mastro
Subject: Re: cl-dolist, dolist, cl-return,
Date: Tue, 7 Jul 2015 17:31:35 -0700

Emanuel Berg <embe8573@student.uu.se> wrote:
> I just wrote the below code.
>
> `cl-return' works for `cl-dolist', but not for
> `dolist' because it hasn't an "implicit nil block ...
> established around the loop". And there is no
> `return'! I think this "cl-" stuff is confusing.
> Anyone cares to explain?

The `cl' prefix stands for Common Lisp, from whence the functionality
there came (or was inspired by).

> By the way, I know there are one zillion loops
> in Lisp. What is the conventional way to
>
>     1. iterate a list
>     2. until some condition is met for some element
>     3. then break, to speak in C

I don't know if there's one most-conventional way, but here are a couple
options:

;; Use `catch' and `throw'
(catch 'done
  (dolist (elt list)
    (do-something elt)
    (when (some-condition)
      (throw 'done t))))

;; Use `while' and a condition variable
(let (done)
  (while (and list (not done))
    (let ((elt (pop list)))
      (do-something elt)
      (when (some-condition)
        (setq done t)))))

> And: With [cl-]dolist, if the list is an expression
> and not an atom, does that get evaluated once, or does
> it happen every iteration like, say, a string length
> test would, again in C?

Just once. Besides inefficiency, evaluating it every time would lead to
wrong results.

> Also: why isn't there a "neq"?

I don't know the real answer, but it may be because then people would
expect `n=', `neql', `nequal', `nequalp', `nstring=', and so on. Or
perhaps some would have a "not variant" and others wouldn't, in which
case we'd need to remember which did and didn't. IMHO it's simpler and
cleaner to have a single `not' which can be used with any predicate as
needed.

-- 
john



reply via email to

[Prev in Thread] Current Thread [Next in Thread]