[Top][All Lists]

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

Re: How to break out of an emacs lisp loop ?

From: Joost Kremers
Subject: Re: How to break out of an emacs lisp loop ?
Date: 23 Oct 2007 19:03:42 GMT
User-agent: slrn/ (Linux)

address@hidden wrote:
> (while (not (forward-char))
>     (if (looking-at "a") break )
> When I run this sexp, I get error at break. So break is wrong syntax.
> But this is the way many C loops are written.

note, the way break appears it, it would be a variable, not a function at
all. furthermore, your (pseudo)code doesn't really make sense: forward-char
moves point, and produces an error if this isn't possible.

however, for what you seem to want to do here, emacs has the function

in general, there are different ways to do what you want. catch+throw is
one way of doing it. often, it is also possible to put the relevant part
inside the while test:

> while ((c=(getchar()) != EOF){ if (c=='a') break; }

(let (c)
  (while (progn (setq c (read-char-exclusive))
                (not (or (eq c EOF)
                         (eq c ?a))))

(note, i actually find this code rather ugly, and would probably write it

it's actually not all that uncommon to find while-loops in lisp that have
no code whatsoever in the body, just in the test.

lisp is of a higher abstraction than a language like C, and it allows you
to do things that aren't straightforward in C, and are therefore not
normally done. so if you run into this sort of thing, don't just ask
yourself how some C idiom is translated in lisp, but also try and think if
there's another way to do what you want, and see if you can express that in

Joost Kremers                                      address@hidden
Selbst in die Unterwelt dringt durch Spalten Licht

reply via email to

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