Author Topic: How do you deal with the "return" and "break,continue,goto" problems?  (Read 2635 times)

0 Members and 1 Guest are viewing this topic.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Sometimes we need flow of program can break in a special point,or change the direction.for other language,we can use "return" or "break" or "goto"(it's not always bad thing,it's useful too),but for LISP, it's a little difficult.I must use a lot of cond,if ,or some other statements ,it makes program hard to understand and complicated.
How do you deal with them?Could you give your suggestions? Thanks very much.
I am a bilingualist,Chinese and Chinglish.

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #1 on: September 01, 2010, 05:40:03 AM »
You probably know about the while-loop:
Code: [Select]
(defun c:LoopTest ( / answer)
  (while (/= answer "Yes")
    (initget "Yes No")
    (setq answer (getkword "\nExit this while loop? [Yes/No] <No>: "))
  )
  (princ)
)

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #2 on: September 01, 2010, 10:35:48 AM »
Thats why for anything more complex than couple of lines I do a program flow outline first, then write up the comments in the LSP file, then code around the comments.  A lot of LSP is self-documenting (well, sort-of...) but its still a lot easier to add comments now than trying to figure it out again a year later.

Instead of massively nested (if...) and (cond...) structures, make separate functions out of the enclosed contents.  This improves readability and consistency through code re-use.  When stepping through code to find problems you can simply step over the function call once you have determined its working properly.
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #3 on: September 01, 2010, 12:22:09 PM »
I develop pseudo code along with any complicated program i create.

Example (cut and pasted from one of the files i was maintaining at the moment):
Code: [Select]
<snip>
                          find layer
                                  locate layer in drawing
                                  { switch
                                        case: if layer !exist
                                              {
                                                      find layer file
                                                              parse layer file
                                                              create layer
                                              }
                                        case: else
                                              {
                                                      { switch
                                                        case: layer locked { unlock }
                                                      }

                                                      set layer current
                                              }
                                  }
<snip>
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

TimSpangler

  • Water Moccasin
  • Posts: 2010
  • CAD Naked!!
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #4 on: September 01, 2010, 12:25:00 PM »
Well commented code.......
ACA 2015 - Windows 7 Pro
All Comments and Content by TimSpangler, Copyright © 2016

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #5 on: September 03, 2010, 04:52:31 AM »
Thanks for all the advice,thanks very much.
I saw a code below:
Code: [Select]
(defun Fsxm-Apply ($Sym $Lst / $$ return $rt)
  (defun Return (var) (setq Return nil) (setq $$ var) (exit))
  (setq $rt (vl-catch-all-apply $Sym $Lst))
  (if Return
    $rt
    $$
  )
)
It can deal with "return",looks very good.
I am a bilingualist,Chinese and Chinglish.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #6 on: September 03, 2010, 04:56:13 AM »

What does that code do 'bird ??

The Return function is never called, so what is it doing ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #7 on: September 03, 2010, 05:52:50 AM »
I show you an example:

Code: [Select]
(defun c:test()
  (fsxm-apply 'func '((1 2 3 4 7 9 5 6 7 8 9)))
)
;;
(defun func (lst / i)
  (setq i 0)
  (foreach n lst
    (if (= n 5)
      (return  i)
    )
    (setq i (1+ i))
  )
  nil
)
you maybe notice that ,the last result returns the right position of an element in a list.
 
Generally speaking ,"foreach " means every element,but in this case ,it's broken by  (return ....), and it will not exit the program,just returns from this function and returns a value.
« Last Edit: September 03, 2010, 06:03:19 AM by highflybird »
I am a bilingualist,Chinese and Chinglish.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #8 on: September 03, 2010, 07:28:01 AM »
Hi,

By my side, I often use a 'boolean variable' in a while loop :

Code: [Select]
(setq lst '(1 2 3 4 7 9 5 6 7 8 9))

(setq loop T
      len  (length lst)
      n    0
)
(while (and (< n len) loop)
  (if (= (nth n lst) 5)
    (setq loop nil)
    (setq n (1+ n))
  )
)
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #9 on: September 03, 2010, 07:40:39 AM »
I show you an example:
< .. >

Thanks, seeing all the code makes a difference :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #10 on: September 03, 2010, 07:51:34 AM »
Appologies, my exemple is a "break" like statement.
Speaking English as a French Frog

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #11 on: September 03, 2010, 07:55:06 AM »
@ highflybird: That's very clever! Pity it will only work with the apply-construction.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #12 on: September 03, 2010, 07:57:28 AM »
Hi,

By my side, I often use a 'boolean variable' in a while loop :
....

You are right. I often use this way.  but sometimes, we wish we can return a value from any place,any time,like C++,so  if  there are a lot of statements after "while",this way isn't convinient.
Use  (exit) can make the sentences more simple,more readable.
I am a bilingualist,Chinese and Chinglish.

highflyingbird

  • Bull Frog
  • Posts: 415
  • Later equals never.
Re: How do you deal with the "return" and "break,continue,goto" problems?
« Reply #13 on: September 03, 2010, 07:59:59 AM »
@ highflybird: That's very clever! Pity it will only work with the apply-construction.

yes,a little pity.
I am a bilingualist,Chinese and Chinglish.