unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to break out of an emacs lisp loop ?
@ 2007-10-23 18:29 gnuist006
  2007-10-23 19:03 ` Joost Kremers
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: gnuist006 @ 2007-10-23 18:29 UTC (permalink / raw)
  To: help-gnu-emacs


(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.

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

as in compilable running file:

#include <stdio.h>
main(){
int c;
  while ((c=getchar()) != EOF){ if (c=='a') break; }
}

So how do I break out of a loop in lisp ?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to break out of an emacs lisp loop ?
  2007-10-23 18:29 How to break out of an emacs lisp loop ? gnuist006
@ 2007-10-23 19:03 ` Joost Kremers
  2007-10-23 20:00 ` Bastien
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Joost Kremers @ 2007-10-23 19:03 UTC (permalink / raw)
  To: help-gnu-emacs

gnuist006@gmail.com 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
skip-chars-forward.

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))))
  (<other-code>)))

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

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
lisp.


-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to break out of an emacs lisp loop ?
  2007-10-23 18:29 How to break out of an emacs lisp loop ? gnuist006
  2007-10-23 19:03 ` Joost Kremers
@ 2007-10-23 20:00 ` Bastien
  2007-10-23 20:10 ` thermate
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Bastien @ 2007-10-23 20:00 UTC (permalink / raw)
  To: help-gnu-emacs

gnuist006@gmail.com writes:

> So how do I break out of a loop in lisp ?

(info "(elisp)Catch and Throw")

-- 
Bastien

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to break out of an emacs lisp loop ?
  2007-10-23 18:29 How to break out of an emacs lisp loop ? gnuist006
  2007-10-23 19:03 ` Joost Kremers
  2007-10-23 20:00 ` Bastien
@ 2007-10-23 20:10 ` thermate
  2007-10-24  1:12 ` Stefan Monnier
       [not found] ` <1193170235.626923.210420@t8g2000prg.googlegroups.com>
  4 siblings, 0 replies; 6+ messages in thread
From: thermate @ 2007-10-23 20:10 UTC (permalink / raw)
  To: help-gnu-emacs

On Oct 23, 11:29 am, gnuist...@gmail.com 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.
>
> while ((c=(getchar()) != EOF){ if (c=='a') break; }

Here is a one/two liner that exactly does what you want with the same
functions that you have and without any break and is actually readable
like an english sentence

(while (not (looking-at "a" ))
  (forward-char))

=============

Watch and spread the link of the movie below. The 911 truthers
confront the VILE and EVIL RABID NEOCON ZIONIST Bastard
Norman Podhoretz at Barnes & Noble.

NYC Confronts Norman Podhoretz!!
http://www.youtube.com/watch?v=3MBzLTjVMhY

from the great site
911blogger.com <--------------------------
http://www.voxfux.com/features/bush_child_sex_coverup/franklin.htm
<-----
http://counterpunch.org/neumann04142007.html
http://counterpunch.org/ross04182007.html
www.911truth.org
www.st911.org
www.counterpunch.org
www.countercurrents.org
www.nkusa.org
www.911blogger.org
www.mujca.org
http://home.att.net/~south.tower/911RussianSatellite1.htm
Loose Change
Terror Storm
Alex Jones - www.infowars.com, www.prisonplanet.org
The Bohemian Grove
The arrest of Alex Jones by bush
Republican Pedophilia - a distinguished record
Monica Lewdinsky aka Bill Clintone
Neocons, Scooter, Judith Miller, Thomas Friedman, Charles Krauthmer,
Edward Bernays
Benjamin Friedman speech
Holocaust Victims Accuse the Zionists
Moshe Katsa and his rapes
Yank Bastards and their 911 HEINOUS CRIME

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to break out of an emacs lisp loop ?
  2007-10-23 18:29 How to break out of an emacs lisp loop ? gnuist006
                   ` (2 preceding siblings ...)
  2007-10-23 20:10 ` thermate
@ 2007-10-24  1:12 ` Stefan Monnier
       [not found] ` <1193170235.626923.210420@t8g2000prg.googlegroups.com>
  4 siblings, 0 replies; 6+ messages in thread
From: Stefan Monnier @ 2007-10-24  1:12 UTC (permalink / raw)
  To: help-gnu-emacs

> #include <stdio.h>
> main(){
> int c;
>   while ((c=getchar()) != EOF){ if (c=='a') break; }
> }

> So how do I break out of a loop in lisp ?

Try to first do it in C: rewrite the loop so it doesn't use break.


        Stefan

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: How to break out of an emacs lisp loop ?
       [not found] ` <1193170235.626923.210420@t8g2000prg.googlegroups.com>
@ 2007-10-24 19:59   ` Jason
  0 siblings, 0 replies; 6+ messages in thread
From: Jason @ 2007-10-24 19:59 UTC (permalink / raw)
  To: help-gnu-emacs

On Oct 23, 1:10 pm, therm...@india.com wrote:
> On Oct 23, 11:29 am, gnuist...@gmail.com 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.

(progn
  (insert "\nbefore")
  (do ((i 0 (+ i 1)))
      ((> i 10) (insert "\ndone"))
    (progn
      (insert "\n")
      (insert (int-to-string i))
      (if (> i 5)
	  (return i))))
  (insert "\nafter"))

-Jason

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-10-24 19:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-23 18:29 How to break out of an emacs lisp loop ? gnuist006
2007-10-23 19:03 ` Joost Kremers
2007-10-23 20:00 ` Bastien
2007-10-23 20:10 ` thermate
2007-10-24  1:12 ` Stefan Monnier
     [not found] ` <1193170235.626923.210420@t8g2000prg.googlegroups.com>
2007-10-24 19:59   ` Jason

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).