Escaping from lisp...

Discussion in 'AutoCAD' started by mr_nick, May 5, 2004.

  1. mr_nick

    mr_nick Guest

    I can't for the life of me get an escape to work in lisp.

    Just as a test, I created a simple loop that counted to 10 and 'princ' ed the number. Thus I get 1-10 output to command line. I then put in a (COMMAND) break, so that if the counter hit 6, the routine should escape. Now when I run it, I get 12345*Cancel*678910.

    This isn't the result I was expecting. How do I get a proper escape that will just quit out of a routine?
     
    mr_nick, May 5, 2004
    #1
  2. mr_nick

    Jürg Menzi Guest

    mr_nick

    (exit)

    or better:
    <snip>
    (setq counter 1)
    (while (< counter 6)
    (princ counter)
    (setq counter (1+ counter))
    )
    <snip>

    Cheers
     
    Jürg Menzi, May 5, 2004
    #2
  3. mr_nick

    DFrank Guest

    You need *2* (command) statements.


    the number. Thus I get 1-10 output to command line. I then put in a
    (COMMAND) break, so that if the counter hit 6, the routine should escape.
    Now when I run it, I get 12345*Cancel*678910.
    will just quit out of a routine?
     
    DFrank, May 5, 2004
    #3
  4. That wouldn't be my first choice.

    I might arrange the logic so it can exit a loop
    without trashing the entire lisp execution. Juerg
    has demonstrated one way. Another way might be to
    establish an exit or boundary condition like so:

    (defun c:demo ( / i boundary done )

    ;; initialize vars

    (setq
    i 0
    boundary 6
    done nil
    )

    ;; informative prompt

    (princ
    (strcat
    "\nCounting 1 .. "
    (itoa boundary)
    " ..."
    )
    )

    ;; count 1 .. boundary, then exit the loop

    (while (not done)
    ;; could use eq or =, though if
    ;; something incremented i an odd
    ;; amount an equality test might
    ;; fail to catch the breach of the
    ;; boundary condition event; the
    ;; <= will catch it regardless
    (if (<= boundary (setq i (1+ i)))
    (setq done t)
    )
    (print i)
    )

    ;; show that we're still alive

    (princ "\nStill alive, now exiting demo.")

    ;;; shhh

    (princ)

    )

    Cheers.

    You need *2* (command) statements.

    the number. Thus I get 1-10 output to command line. I then put in a
    (COMMAND) break, so that if the counter hit 6, the routine should escape.
    Now when I run it, I get 12345*Cancel*678910.
    will just quit out of a routine?
     
    michael puckett, May 5, 2004
    #4
  5. Another alternative to the actual loop portion might
    be:

    (while (not done)
    (if (< boundary (setq i (1+ i)))
    (setq done t)
    (print i)
    )
    )

    The important thing is there are a number of ways
    of doing this; many of them give you the ability to
    keep execution alive (funny words when together).

    That wouldn't be my first choice.

    I might arrange the logic so it can exit a loop
    without trashing the entire lisp execution. Juerg
    has demonstrated one way. Another way might be to
    establish an exit or boundary condition like so:

    (defun c:demo ( / i boundary done )

    ;; initialize vars

    (setq
    i 0
    boundary 6
    done nil
    )

    ;; informative prompt

    (princ
    (strcat
    "\nCounting 1 .. "
    (itoa boundary)
    " ..."
    )
    )

    ;; count 1 .. boundary, then exit the loop

    (while (not done)
    ;; could use eq or =, though if
    ;; something incremented i an odd
    ;; amount an equality test might
    ;; fail to catch the breach of the
    ;; boundary condition event; the
    ;; <= will catch it regardless
    (if (<= boundary (setq i (1+ i)))
    (setq done t)
    )
    (print i)
    )

    ;; show that we're still alive

    (princ "\nStill alive, now exiting demo.")

    ;;; shhh

    (princ)

    )

    Cheers.

    You need *2* (command) statements.

    <snip>
     
    michael puckett, May 5, 2004
    #5
  6. You can't use "(command) statements" to
    break out of a LISP function.
     
    Tony Tanzillo, May 5, 2004
    #6
  7. mr_nick

    mr_nick Guest

    The idea of my example was not to simply count to 6 as people seem to be
    working to. This was a very simplistic approach to my problem. What I want
    to do is carry out a full escape from my routine if a certain condition is
    met. I have routines nested inside routines and if a condition is met in one
    of these nested routines, I need to be able to escape completely - not just
    out of the nested routine - just as you would if you hit the escape key. The
    fact that I couldn't get the escape to happen on such a simple thing as the
    count was just to prove to myself that it wasn't that simple. I have tried
    the EXIT method, but for some reason, this keeps throwing up an 'error in
    *error*'. Bit confusing since at this point I'm not using any kind of custom
    error checking :eek:(

    Is there a way of doing such a thing?
     
    mr_nick, May 5, 2004
    #7
  8. mr_nick

    Jürg Menzi Guest

    mr_nick

    'exit' *is* a unfriendly break of code execution,
    but normally you don't need it...
    Another way to avoid 'exit' -> apply conditional
    checks:
    (cond
    ((not (ReadCfgFile))
    ((not (InitializeReactors))
    ((not (ImportLanguageProfiles))
    (T (DoAllNecessaryScrapOnStartUp))
    )

    Cheers
     
    Jürg Menzi, May 5, 2004
    #8
  9. mr_nick

    Jürg Menzi Guest

    Oops..

    must be:
    (defun MyStartInit ()
    (cond
    ((not (ReadCfgFile)) (alert "Config error..."))
    ((not (InitializeReactors)) (alert "Reactor error..."))
    ((not (ImportLanguageProfiles)) (alert "Language error..."))
    (T
    (DoAllNecessaryScrapOnStartUp)
    )
    )
    (princ)
    )

    Sorry
     
    Jürg Menzi, May 6, 2004
    #9
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.