Author Topic: Possible to return from middle of loop?  (Read 3991 times)

0 Members and 1 Guest are viewing this topic.

SIDESHOWBOB

  • Guest
Possible to return from middle of loop?
« on: January 31, 2012, 12:34:29 PM »
Another noob question!

Is it possible to return a value from the middle of a loop (as one would with (return 'foo) in common lisp)?
Or do you have to handle such things in the loop's termination condition?

Or ... should I be using recursion instead for a task like that?  Is tail recursion handled efficiently by the autolisp interpreter?

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Possible to return from middle of loop?
« Reply #1 on: January 31, 2012, 01:23:06 PM »
There is probably an explanation of loops around here but no time to look now.

Quickly, the WHILE loop and vl-some will exit the loop conditionally.
The FOREACH and REPEAT and vl-every and mapcar will loop until the every item in the list supplied.
For these loops you will need to set a variable for the condition you want to return.


The WHILE loop is very versatile and you may design it to exit when your condition is met although it will
still require you to set a variable.

I'll look later for the examples if no one post them.

gotta go.
« Last Edit: January 31, 2012, 01:29:43 PM by CAB »
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: Possible to return from middle of loop?
« Reply #2 on: January 31, 2012, 04:18:47 PM »
Or do you have to handle such things in the loop's termination condition?

There is no equivalent 'return' function in AutoLISP, so yes, that is what I would recommend.

Or ... should I be using recursion instead for a task like that?  Is tail recursion handled efficiently by the autolisp interpreter?

Tail recursion could be used, but to my knowledge, it isn't optimised in AutoLISP as it is in other languages.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Possible to return from middle of loop?
« Reply #3 on: February 03, 2012, 09:19:52 PM »
Exiting a loop prematurely is considered a sign of poor programming in most circles.

To exit a loop without going through every item or every n, you should incorporate a boolean short-circuit condition.

This will require setting a variable that causes the loop to end.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Possible to return from middle of loop?
« Reply #4 on: February 03, 2012, 09:24:55 PM »
Exiting a loop prematurely is considered a sign of poor programming in most circles.
< .. >

I have to say I'm biting my tongue over this statement.
I assume it is an atempt at humour.
 :|
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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Possible to return from middle of loop?
« Reply #5 on: February 03, 2012, 09:42:55 PM »
Exiting a loop prematurely is considered a sign of poor programming in most circles.
< .. >

I have to say I'm biting my tongue over this statement.
I assume it is an atempt at humour.
 :|

Not at all.
There has been much debate over whether it is proper to exit a loop prematurely (i.e. the loop condition is not met).

The example I am talking about is something like this:
Code: [Select]
for (int i = 0; i < 100; i++)
{
   switch (some_int)
   {
      case 1:
         return item1;
      case 2:
         return item2;
      case 3:
         return item3;
   }
}

The arguments I have read for using these kinds of short circuits is that if the language didn't want you to use multiple returns, there would be an error of some kind, while those advocating against using them, say that a loop should have a condition that causes it to end and then return the value.

I really don't care how someone does it. But I do know that if someone can't understand what the code is doing, it makes it infinitely more difficult to debug and/or edit.

Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Possible to return from middle of loop?
« Reply #6 on: February 03, 2012, 10:52:21 PM »
< .. >I really don't care how someone does it. But I do know that if someone can't understand what the code is doing, it makes it infinitely more difficult to debug and/or edit.

That seems to be an argument for coder proficiency rather than against prematute evacuation.
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.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Possible to return from middle of loop?
« Reply #7 on: February 04, 2012, 12:14:30 AM »
< .. >I really don't care how someone does it. But I do know that if someone can't understand what the code is doing, it makes it infinitely more difficult to debug and/or edit.

That seems to be an argument for coder proficiency rather than against prematute evacuation.

I don't disagree with you, but the problem is much deeper than that.

If a programmer cannot write effective code that gets them the answer they are looking for without forcing a premature exit from a loop, I would suspect that the programmer doesn't have a firm grasp on the logic needed to be an effective programmer.

Certainly there are times when it is perfectly ok to exit a code block without reaching the end. In fact, if you look at the ASM output of just about any decompiled application you will see that it is littered with jumps into and out of code blocks.

I think the whole concept comes from a structured programming perspective ... that being that a code block (function, loop etc) should have one way into it and one way out of it.

But then programmers are also a lazy lot .. digging for efficiency, not only in code execution, but also in the number of keys they have to pound on to get a result.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Possible to return from middle of loop?
« Reply #8 on: February 04, 2012, 04:21:26 AM »
Here is some code that will continue itterating a list until it finds a true response to the test
ie : find the first number in a random list divisable  by 12

Code - Auto/Visual Lisp: [Select]
  1. (setq lst '()
  2.       return nil
  3. )
  4. (repeat 500 (setq lst (cons (dos_random) lst)))
  5.  
  6. (if (vl-some (function (lambda (x) (setq return x) (equal 0. (rem x 12) 1e-6)))
  7.              lst
  8.     )
  9.   return
  10. )
Quote
(25502 28128 11381 4755 18686 31250 23390 21085 20083 8187 21109 17651 29986 28641 25395 14676 12570 6840 26412 30066 2503 25089 26 17975 9364 3818 40 11447 17889 27809 9858 17589 5158 506 7195 16474 10599 1604 23743 5427 16090 19814 30811 13930 32469 10048 22667 30792 18684 28223 30308 30953 29866 16704 11393 10816 23967 9385 4434 22861 18154 1731 1443 20123 24599 25436 25449 25598 30875 669 30931 11149 11647 3879 15248 989 7498 32627 24668 13694 28211 3690 17103 30137 12933 16193 18792 27273 30548 10393 12759 3034 2401 726 29065 32255 15433 13758 5752 16760 16371 24917 21555 21476 20538 7670 10538 7746 19161 28596 18493 19567 30614 10481 12935 27098 3760 14398 11622 24459 18828 7540 205 12118 2461 530 10700 2138 24979 12504 27999 4287 3271 26787 30581 175 8421 18058 29250 27283 1497 17209 16840 19601 32765 32651 20386 18245 22659 23661 4883 5566 28703 6640 16717 8890 18423 31151 20997 22307 19662 8290 17461 32203 28782 12993 20274 22630 23893 27037 27633 16840 20643 6459 25236 31071 26177 26610 18490 29681 25428 12295 6407 19864 19172 18630 23152 14589 18631 13028 29130 2003 16081 25619 6932 10389 65 11749 11267 13698 1892 31717 3917 28888 12686 25841 9273 19022 19770 19420 28262 18902 1662 2883 16857 31635 32179 26798 22348 14615 5220 19178 29876 27616 15477 28580 5651 9492 21911 7240 17022 9850 25834 1563 31088 14522 13043 28053 25679 27352 7103 10498 14398 5869 7511 6747 29731 18470 28773 19552 26363 24065 14118 13049 10844 2244 3730 19149 32074 31552 3529 5154 11069 28102 32703 9462 27504 20073 25385 20061 25710 14964 31913 4380 16345 7469 1847 23941 31357 4042 20178 23188 23699 22194 16742 12361 18008 6135 16895 4422 15568 10002 18990 32683 25302 479 9658 2352 19047 23258 21641 30338 18990 19611 24271 10722 17233 4750 10103 10490 893 25333 2257 3585 12078 17954 18300 23569 24642 1775 10133 29383 25791 6272 12319 7450 18915 3388 26803 30591 8044 31495 7531 10556 7975 26819 29962 11531 9405 8456 25967 9458 8636 1570 21051 30048 31203 17226 25023 9099 29617 30502 21547 13063 10542 13189 26867 32425 9467 28245 7811 18588 6038 16360 22566 32195 20722 2663 13874 26705 27189 26260 11382 19877 19410 29126 8935 13162 31690 22816 11400 18413 24637 15891 8346 10687 25141 20502 8707 2702 7349 14243 24834 26943 1023 11891 8978 24399 12581 24828 23838 15722 15826 29993 27621 29515 23639 21083 13998 32468 6139 1695 18479 24176 27005 25903 10681 17063 3053 14893 3239 31490 15787 12189 3591 23072 22974 16290 8329 3351 30041 20476 26790 8719 28074 21184 5907 14875 4580 2136 3432 8793 21901 16060 22795 20556 14091 10737 20577 32119 18028 12737 24118 24125 30836 20667 18283 12279 12779 24684 30432 6741 14119 28382 7075 8184 27606 31056 9561 6734 3855 10293 27372 31920 24474 26737 1908 23224 21630 32209 14697 28135 21963 27338 20489 5750 8616 18805 29445 4987 18846 29130 20594 28223 29733 2330 31981 21724 20078 22225)
 
28128

Quote
(30783 14469 6538 7887 1897 11979 6878 32506 22162 8982 2208 22860 32261 25329 2270 26685 12033 16366 22688 29421 22241 10559 10428 10471 4229 146 26333 12054 12294 26160 24272 14854 29849 30806 24593 20830 11458 16853 21396 6300 22716 21479 28119 14609 5927 18259 26919 18718 26610 8388 29744 19631 4489 21604 16586 10998 13377 25790 24320 13675 22530 22740 20580 7171 53 1405 32657 27311 4592 26115 2047 27671 6238 32764 31245 3477 13171 3687 1866 8310 15567 28681 17157 5230 28402 5385 25367 1883 6816 26569 11503 17981 13973 27909 31453 7028 21867 5936 1441 1595 19914 2317 24117 14179 8400 17291 17369 28498 27330 31461 16991 6461 27123 3832 17591 16292 26611 25318 1981 13837 4346 4074 6464 5123 1430 12157 20308 23888 12946 14504 25563 29561 15144 11434 8272 11385 361 3789 17877 10943 6379 25476 7551 21100 12212 13000 23775 30664 14693 18866 19154 8964 26792 14171 20620 1334 17117 16626 25314 31376 23043 18717 28630 13793 30947 26260 15463 29007 19842 9722 24216 16469 24007 13053 16320 24314 21098 25776 21267 6738 25631 26919 27760 11833 30466 3154 25452 13388 12356 22045 28317 42 4629 31687 25386 1259 27091 24705 17553 8160 7283 8522 20666 25739 7470 26632 29756 17887 6766 489 21229 14523 11514 29947 20654 11171 5471 4360 18038 28607 21409 14262 20383 28440 9505 22494 23407 19472 13519 22570 28392 17953 2451 27892 17107 32729 32503 5469 10761 532 21528 15275 27651 6266 7373 13543 32676 25402 8472 7093 21060 14855 30412 17013 1790 15441 24958 14489 23101 10951 17584 8007 5507 26124 13370 18738 16251 11959 28411 25012 29761 656 20415 5934 4591 13063 113 1187 9578 4343 31620 22161 17260 18842 10142 26385 14804 20686 6533 19761 17708 29833 18509 7056 13946 30104 16321 19002 13642 16798 7441 1988 2554 10827 25020 28074 7239 13787 26814 21478 4632 25808 2556 16339 14984 2290 14318 10819 12600 2438 9102 6366 26068 12493 12927 18108 29669 24254 21376 22597 30424 15621 32018 22086 17998 20542 25190 27734 18085 621 10891 9834 14790 704 13784 10677 15407 5767 15765 9412 23105 14193 23024 29400 27085 6923 14878 11909 5084 32629 6862 12170 15038 22885 18133 12274 10324 16224 11798 5227 12448 11829 24241 5093 13452 700 27759 14957 12536 17615 4715 841 17929 8072 6915 1623 25264 8891 23041 29857 119 10677 17668 24643 7058 821 30259 2944 21284 13993 9507 15137 24016 684 25095 18448 16002 28862 7961 27700 25981 6724 21575 1677 10663 31426 18721 21138 17939 27478 20736 20169 18382 9078 1401 20134 6125 23277 19203 29608 3542 26897 11238 2480 13773 32072 21862 675 14272 19645 17199 3394 2645 20829 25858 839 24115 30231 19103 10946 28047 19539 8692 1717 7162 9905 4998 13037 4712 13573 18718 1825 17282 32145 10935 32080 1071 20356 7590 5974 9310 2737 21001 16334 13336 22675 27251 25134 24773 12927 2520 13855 5821 20778 5045 31260 21265 24925 10883 6478 20889 6646 14428 30885 832 3872 3565 26424 30862 24277)

2208



Quote
(10758 12489 21411 8979 11698 19468 14286 10102 555 6227 23917 28356 17204 8624 9227 19785 24514 21705 4949 14578 11245 7741 24743 27844 25880 14651 25813 4510 13406 10591 11836 2097 31550 7118 6923 12351 30345 11156 10189 18706 906 17723 24122 18238 4982 9647 4157 15258 6421 12368 19271 10106 31920 6231 12245 23088 25149 6508 6463 13252 19911 15238 16833 26865 29218 24069 20173 9314 13774 1549 29074 8493 14890 11299 1990 6798 28892 1293 26811 11495 2943 484 19104 1014 20877 893 27669 29271 10382 31993 28148 20027 23168 424 2397 12137 26248 31269 3586 27252 26584 31918 21683 25917 24290 1219 4651 8363 26814 1537 13851 5032 12503 5695 17014 13149 10459 10984 18174 17584 21219 9625 27918 21244 10720 11262 16296 26641 29768 1768 7104 24942 3328 1201 29897 32444 28299 6867 22912 1421 3187 17093 25846 5717 18166 19991 2019 3751 1267 11728 22056 5299 16405 26886 17571 10238 3904 16061 826 21346 31547 12036 24353 23623 10654 26444 22892 2850 10748 19978 24028 26925 29011 10639 17893 3943 20403 7717 5117 25293 27692 32621 4907 23000 4860 12534 18125 10041 25079 4640 22187 26067 7993 12594 25403 8695 17929 16824 28213 23525 5646 4641 17241 24450 12472 31896 3454 3413 4327 3873 990 28870 26847 26358 23455 16280 12911 31522 27043 7846 32094 21086 8339 18396 18457 24618 22312 28023 28313 1325 698 30335 31641 28368 21006 28804 26615 21016 21595 21143 5225 15811 6300 28042 2403 32677 28956 9578 18759 22850 18237 20540 16589 32686 11146 22005 8627 8867 3582 28094 13224 30435 19093 5224 26484 32719 30227 6188 14443 5191 5906 21926 10258 23234 14161 530 24593 14512 31034 7879 18011 28942 7603 29171 3995 28233 9768 8343 7137 29527 32471 17860 10635 12306 23901 8982 10872 29910 16256 18032 18281 348 13885 16633 23994 10968 26900 19285 833 21840 32145 29478 23684 31805 20253 11715 20769 18023 8965 27705 27256 24828 17610 3202 26781 7665 13993 17411 22908 4508 25197 24395 15948 11153 30323 29448 2872 1723 23292 23115 3402 27837 27577 7679 666 18569 24979 8613 28854 1088 6329 12628 1363 3659 7918 19695 3380 441 13843 19623 19801 11738 14066 15026 19050 31004 75 32331 19044 23332 24099 13293 7301 27182 5450 20170 29556 14248 32557 24464 23987 30731 25052 7262 10312 12796 28765 1539 14248 28834 7986 18040 8934 13640 14631 9375 7452 9270 24671 10555 31000 10443 26528 20785 21321 3293 12253 26880 11754 16211 30211 28029 2927 17922 15623 3537 21295 5831 16430 10935 3154 21476 6558 28400 15100 10142 2340 23817 27577 15127 1862 28240 14633 1127 4235 7954 22511 26624 21768 1048 29909 26946 14381 17762 28995 26203 19664 6693 18913 7011 14713 8705 13194 26807 12527 14214 14962 6292 27393 6120 28569 8182 18840 31744 28786 5809 5628 32389 21710 25296 613 27530 20237 21292 23492 21817 2695 9551 6097 2512 5174 12937 11094 18140 22951 16466 21693 17843 2593 12258 18135 32053 26134 2317 24557 13509 18633 7266 22795 17540)

28356

Quote
(16754 27038 5193 28621 19982 6450 5610 13101 9425 6388 23116 19066 29182 21617 10790 25584 1936 16806 28082 6050 2534 8729 9984 3760 29361 10036 8315 17350 13852 13795 15290 14550 5692 17776 3210 15896 28856 5532 11323 9793 4795 14336 28970 1025 24346 10405 30350 19649 21184 29153 8326 31901 13152 6003 2145 6046 29789 17632 32046 9275 7666 31502 1157 31505 17064 14624 1943 2016 20217 23700 6259 22471 16191 13465 31641 4291 2018 1205 25027 11876 15296 29334 32240 8144 15581 6966 25524 8956 846 19252 22974 31015 18018 5745 3241 20633 5137 19176 19974 32668 8872 31441 19368 27834 26804 6648 27441 16439 18222 27224 13787 15055 20967 13310 138 19891 565 1052 15377 20416 24541 28522 6595 27649 27714 5059 28733 9574 23501 21759 3184 3012 27038 4364 23283 32271 20851 18259 5513 5589 15319 28488 24816 6140 10382 31313 1771 1727 9023 15579 14481 12516 20065 15032 1238 23105 4202 4798 28504 22897 28469 1436 21358 18129 26034 21501 32621 6336 16339 28642 13371 18709 18613 15675 29977 24547 22284 24007 21419 32618 979 17452 9290 19808 5939 2647 18061 21856 30643 18200 28180 16561 10144 24312 6038 32570 7357 3164 5676 15812 32301 6879 25822 17776 5279 4586 10350 8314 30906 19095 3426 8239 19402 11462 28246 8234 1707 3967 28290 8917 10996 30858 29832 8990 25191 14155 12674 14199 22678 5857 8266 25107 23256 9088 10506 10845 2670 16141 21766 23483 11504 250 16040 1411 26364 7523 10700 6204 27936 20328 28747 15384 30470 716 11980 15536 225 14541 24008 9729 10006 25668 17118 28325 10285 14590 26200 19285 22777 10844 30971 1949 5893 5416 22037 32736 1344 20666 3523 6286 7915 9034 9090 20130 27841 21909 27896 15712 26727 25013 15618 13647 23061 31036 18175 16499 15492 24209 26206 12239 29465 19854 13924 3609 24146 28505 9793 5053 6767 5506 30037 30742 29795 14428 17342 15338 24828 29855 8996 18725 592 1084 21447 985 1898 28290 6599 10357 29236 27505 22197 4931 4006 13917 21337 19750 11822 19397 26053 31122 24160 20084 24473 462 18913 4055 28822 6504 27792 30624 26984 30712 15349 18075 2734 25624 2286 28878 19533 27553 10134 2774 18677 25897 10615 1663 14835 5820 13810 15012 12087 7640 19692 29256 28583 29869 32701 32499 11684 12437 27732 2641 31708 21968 1710 8968 11857 23683 31864 16600 5033 17137 16129 15351 12983 18682 32612 29613 30792 12499 10485 8237 304 8454 9707 2997 27799 940 31310 12008 25215 32488 289 27754 15000 25253 22482 26555 28487 10130 17377 13143 18675 23388 7886 21005 28049 5677 5190 18335 16497 21860 16016 13465 15945 22421 27956 14709 16769 21954 8684 7406 23231 13785 4047 29091 10677 4719 10426 14062 25128 13245 1280 12549 26947 21979 12628 29777 14108 22586 12021 24588 26758 12902 12826 1056 11544 18939 27296 11838 32125 1511 25328 6716 17916 15072 16693 435 29001 19751 30397 1751 32652 9085 10670 13976 5553 18277 25836 7672 12529 20122 23112 11950 27296 27089 17132 28410 30775 13490)

25584



(21364 4983 9094 3987 21074 7358 11320 26467 17182 23092 3332 25713 23656 5028 24120 15561 10557 9818 22858 32702 15505 17408 21480 15590 24300 2232 29503 6231 16983 25722 17295 26658 2183 5038 8174 14095 10057 8971 5722 18585 30511 15024 13316 31274 30843 11395 26074 7578 6731 456 22869 10468 4101 1028 2435 22313 259 32019 25686 5865 7809 10886 13357 24959 27590 8257 14753 22492 17552 5715 8596 16519 28099 24087 17271 17107 15444 31371 21465 31585 7793 6126 30519 17879 4852 24223 24833 22496 23305 23859 27370 2406 29213 1495 20595 26126 6153 31463 31417 25587 31102 9934 2173 23461 17073 23327 32371 7523 10313 29427 19406 16278 12936 13444 3850 655 22199 28168 18170 25988 10966 2514 23572 18631 16259 5541 11771 14663 27689 31066 4429 16715 6508 22708 27053 23147 21970 23681 19473 23762 16565 24308 21839 1969 5991 14145 8421 32517 28838 8313 3079 23179 29249 30630 12525 8706 3338 19752 18012 21154 2814 1360 20340 18908 31665 1085 18364 12626 19198 8437 14186 18456 3566 23495 23542 9515 20967 5086 20703 20272 31256 14376 25790 17775 30499 12136 1675 22033 25867 29591 30510 23960 23391 7298 11080 19359 31654 21920 2370 2257 2572 31539 28564 6702 7245 23202 12121 7547 10976 21011 26386 31496 4198 19278 27065 26792 17236 19919 7688 6636 21982 9260 521 14752 2085 20648 16096 2186 30883 19735 30935 15713 16085 20979 18345 522 31785 8381 786 10777 12073 3480 20373 32389 21735 17442 6030 15020 9387 917 23675 5769 29146 19997 3205 31283 39 18298 18516 13976 22015 16088 17683 24441 22832 8172 14016 8871 16859 18258 28343 14462 12187 30245 6784 18848 27892 24798 20183 26101 11520 5488 19195 26850 20311 13475 8250 26312 30270 15023 10684 32702 31506 21276 142 13201 3318 29656 22512 4858 1466 15255 28130 27168 16653 20337 31723 16464 485 13240 21547 18683 12844 23577 5794 8686 2222 28406 10945 31447 24898 30721 26928 502 21302 1563 8672 12833 20990 28781 27219 24061 25631 16070 24618 5242 15447 28981 19438 14990 16657 8938 20514 30260 19975 17480 4231 18086 15415 26701 32176 26072 31754 24870 12488 20279 19904 10252 930 1954 25633 19555 20736 679 32097 26133 2343 16641 15955 18217 1697 1105 23504 26793 20307 13338 24168 20149 11714 20481 3145 24546 20128 2884 17774 27607 20124 28026 24075 23849 25706 5259 17207 21108 4879 29681 30169 6844 4208 11567 10199 20741 19485 23953 1915 4912 16984 32036 1320 4120 1899 10999 7997 26859 14122 658 71 8652 13509 22197 31597 7592 26321 27988 13079 24919 23448 6354 30175 6645 415 19067 7611 8833 24696 31302 32106 1206 32200 32715 10270 12584 23194 14464 8042 27297 13540 5946 13165 8380 31065 30946 8510 13762 4464 23158 15257 27587 31155 19522 10698 29942 19531 15966 2264 25429 11211 617 31476 29554 14907 7813 28963 28318 2151 19861 15145 8252 10600 18613 10679 6753 22129 13817 20183 17839 20189 22632 10390 30833 24696 23865 16883 4345 17420 25167 5638 20280 14895 4593)

5028



Quote
(13232 28900 24837 13341 23791 5794 14058 7158 1654 8545 26197 14341 25519 2469 8275 32190 2946 7619 9988 1683 24196 23175 5470 29356 19298 20229 9023 19685 15758 18366 20920 19463 16938 15606 11158 3400 12083 16467 1588 9223 22769 26040 28287 6850 8606 25442 11849 16731 28499 25923 5584 25739 21561 20381 30096 19109 29935 27873 29768 4493 31033 17096 11593 18938 4841 2737 17157 27626 26385 2364 20949 101 25960 16678 28428 7285 23035 15718 8252 2403 17686 28295 30406 13877 17780 22342 15271 20070 8357 1135 22399 4583 13758 3059 25316 19029 26182 3916 2792 31098 16735 32020 13799 7432 32050 3554 5268 9107 2718 14439 2885 28366 20259 6344 8061 32521 20747 1852 21945 9193 25050 23016 11134 20072 4854 3576 6562 24591 153 21505 19160 4977 16117 15253 29313 23017 30119 31270 28592 19094 30049 2334 10365 32405 21457 5950 18829 30336 22999 2357 21984 25893 20454 1411 13020 31626 29881 18319 7321 4640 10809 13830 11886 19269 9388 30837 27579 2815 10273 2088 29942 24565 8217 30787 21754 14767 7545 28960 4750 2064 1711 6890 29235 8211 1570 29562 5169 452 734 19312 23267 23425 12158 26260 17728 26224 31579 31597 10668 30786 29173 3617 32000 26152 21665 25230 4566 5396 31347 423 31768 21300 17004 3603 1226 22186 9190 25603 23061 32143 10558 16467 26518 11049 24769 27362 744 24169 4059 8245 22638 23978 13438 13896 17508 5032 8399 14518 1064 31165 15638 30732 27587 11105 15333 18536 3005 32482 19593 27479 29472 16226 6064 24815 31180 25616 4698 7210 16125 24745 29444 22148 18456 27440 21778 32471 27580 21228 22139 32628 29959 17569 15290 22405 11047 6161 26099 17312 10532 692 10083 29102 12779 19381 16909 31741 27804 23855 7328 10146 13590 20135 31149 8724 27842 17611 15625 14852 30623 17004 2942 20353 20877 18104 20664 30538 26588 21691 30574 3445 8983 16406 24526 13120 19724 23296 24620 29749 16158 24956 23532 3716 1229 15835 15535 6811 24196 628 22972 3592 20645 13472 6576 21907 10961 17659 23144 661 13179 20695 27437 18333 25518 2307 9503 16453 24348 22972 28134 16463 5226 12274 12438 9734 12729 30654 6369 27602 22902 12322 7299 29979 17253 12289 7912 21090 13803 30816 15137 30876 922 12254 8 10193 30977 9072 10929 31988 26291 26720 11026 27395 13208 31622 32501 5985 9074 31175 12060 21873 15793 19917 31413 15733 121 28422 31999 6824 19443 3554 22844 11417 12755 18759 14552 30665 5416 18775 10202 8582 18026 23396 7049 4872 1984 27442 12129 24524 9616 30062 25656 11056 3915 20333 28074 2382 7288 24616 22988 5915 27137 9846 7952 17858 31676 10378 23669 5436 3644 10415 22514 14899 17623 15169 1827 27416 8186 6637 11827 20609 26539 20523 24844 21702 30301 7419 29372 20189 4090 9694 7976 17524 12589 22294 31478 16208 751 3360 30134 29156 24147 25456 31314 10035 3220 17518 31159 14825 12126 5808 27587 3766 32091 9079 4720 19948 16128 4718 2880 22619 14231 5792 31467 13163 26255 10425 24390 26746 19714 27264)

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

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Possible to return from middle of loop?
« Reply #9 on: February 04, 2012, 07:15:36 AM »
A good example of such a loop is one which tests a list of values for a specified condition. E.g. test if all the integers in a list are even. Now there are several ways of going about this, some less efficient (both execution & programming) than others.

There's 4 major possibilities: ForEach / While / Recursion / VL-Some:
Code: [Select]
(defun TestEven-Foreach (SampleList / StillEven)
  (setq StillEven T)
  (foreach item SampleList
    (setq StillEven (and StillEven (= (rem item 2) 0)))
  )
)

(defun TestEven-While (SampleList / StillEven index)
  (setq StillEven T
        index     (length SampleList)
  )
  (while (and StillEven (> (setq index (1- index)) -1))
    (setq StillEven (= (rem (nth index SampleList) 2) 0))
  )
  StillEven
)

(defun TestEven-Recursive (SampleList /)
  (or (not SampleList) (and (= (rem (car SampleList) 2) 0) (TestEven-Recursive (cdr SampleList))))
)

(defun TestEven-VL-Some (SampleList /)
  (not (vl-some '(lambda (item) (/= (rem item 2) 0)) SampleList))
)

Which would you think is the most "efficient" to write and to execute? Well execution-wise, theoretically the foreach runs in order N (i.e. it iterates all the items even if the 1st was already uneven). The while iterates only up to an uneven (or end if none) - so this seems faster for the average case. Recursive & VL-Some has the least extras (temporary / state variables) and also uses no setq's - both also stop once an uneven is found so shouldn't be too different from the while. One caveat in AutoLisp is that recursion doesn't have much tail optimization (i.e. each call is placed on the stack and not cleared at the end), also arguments are passed by value (thus making a copy of the list for each "iteration" - could mean order N^2 ram usage), but worst is that ALisp has around 20k limit on nested recursive calls - so if your list is around 20K+ items you could expect an out-of-limit error.

So for my money I'd have "thought" it would be a toss-up between while & vl-some. Though looking deeper, I don't particularly like the (nth index SampleList) call ... that might slow down the while. Thus here's an alternative while:
Code: [Select]
(defun TestEven-While2 (SampleList / StillEven)
  (setq StillEven T)
  (while (and StillEven SampleList)
    (setq StillEven (= (rem (car SampleList) 2) 0)
          SampleList (cdr SampleList)
    )
  )
  StillEven
)
And now for empirical tests, lets start with the extremes:

256 even numbers:
Code: [Select]
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (TESTEVEN-VL-SOME ALLEVEN).......1778 / 2.9 <fastest>
    (TESTEVEN-WHILE2 ALLEVEN)........2667 / 1.93
    (TESTEVEN-FOREACH ALLEVEN).......2824 / 1.82
    (TESTEVEN-RECURSIVE ALLEVEN).....3323 / 1.55
    (TESTEVEN-WHILE ALLEVEN).........5148 / 1 <slowest>
Already I can see I was right about the nth function. Yet also the vl-some & while2 doesn't give too much surprise - though the recursion seems a bit slow IMO. But this one is stacked in favour of foreach, since all have to run through the entire list anyway. And the difference between the while2 and foreach is extremely slight - take note that on another thread I've shown that there are variation if you compile the lisp to FAS/VLX.

Now for the 1st while's favourite, a list of 256 int's with one of the last being uneven:
Code: [Select]
Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):

    (TESTEVEN-WHILE LASTUNEVEN)..........1217 / 10.67 <fastest>
    (TESTEVEN-VL-SOME LASTUNEVEN)........6989 / 1.86
    (TESTEVEN-WHILE2 LASTUNEVEN)........10624 / 1.22
    (TESTEVEN-FOREACH LASTUNEVEN).......11279 / 1.15
    (TESTEVEN-RECURSIVE LASTUNEVEN).....12980 / 1 <slowest>
Yep, it blows the socks off all of them! Even the 2nd while and the vl-some. I think because it's running into the uneven at such an early stage (due to it searching from the end instead of the beginning as the others do). Yet still the vl-some and while2 out performs the foreach and recursive - though not by much.

Now for one where the uneven number is around the start of the list:
Code: [Select]
Benchmarking ....................Elapsed milliseconds / relative speed for 131072 iteration(s):

    (TESTEVEN-WHILE2 1STUNEVEN).........1950 / 20.87 <fastest>
    (TESTEVEN-RECURSIVE 1STUNEVEN)......1965 / 20.71
    (TESTEVEN-VL-SOME 1STUNEVEN)........4789 / 8.5
    (TESTEVEN-FOREACH 1STUNEVEN).......11888 / 3.42
    (TESTEVEN-WHILE 1STUNEVEN).........40700 / 1 <slowest>
Here the while2 comes into its own (as does the recursive). I think the vl-some uses most of its time in the to-ing-and-fro-ing with ARX - thus it takes more time to copy the list to the ObjectARX compiled code than simply checking a value. As expected foreach is out of its league here, but the 1st while is even more so!

Now for a more "average" case ... using 255 even numbers with an uneven placed in position 128:
Code: [Select]
Benchmarking .................Elapsed milliseconds / relative speed for 16384 iteration(s):

    (TESTEVEN-VL-SOME MIDUNEVEN).......1201 / 2.57 <fastest>
    (TESTEVEN-WHILE2 MIDUNEVEN)........1450 / 2.13
    (TESTEVEN-RECURSIVE MIDUNEVEN).....1748 / 1.77
    (TESTEVEN-FOREACH MIDUNEVEN).......2138 / 1.44
    (TESTEVEN-WHILE MIDUNEVEN).........3089 / 1 <slowest>
This seems in line with my first thoughts. The 1st while is a bit of an anomaly due to the nth function call, but the while2 shows up the inefficiency of the foreach approach. The vl-some takes advantage of compiled code efficiency - but performs much the same idea as in the 1st while (though I think it might use an array which works much nicer with an index than a linked list).

In this example I'd go with the vl-some, since it's the most consistent (not to mention a whole lot less typing  :kewl: ) - if pushed (e.g. only ALisp) then the while2. I'm a bit sceptical of the recursive, and since it's all over the place in extreme cases it's not ideal.

Of course (as the 1st while hints at), the internals of a loop has much more effect on efficiency than the loop itself. So some scenarios might just call for a different implementation inside the loop instead of a different type of loop, while others might benefit from a foreach. And as I've alluded to the foreach does get a bit of optimization if you compile it - which could shuffle the cards around a bit.

I'm not going to discuss the "correctness" of placing a return/goto/jump call inside the foreach (as the OP is basically asking), but that would make the for-like loop a lot more efficient now wouldn't it? Unfortunately the only way to even get near such in ALisp/VLisp is to use the vl-exit-with-value function, which only works by exiting out of a compiled VLX (or so the help states) ... so you have to make a VLX with its own namespace simply to be able to return from somewhere in the middle of a loop!  :ugly: Unfortunately there's no return/goto/jump in either ALisp or VLisp, but that might be a "good" thing in that it forces you to write more "understandable" code.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

VovKa

  • Water Moccasin
  • Posts: 1632
  • Ukraine
Re: Possible to return from middle of loop?
« Reply #10 on: February 04, 2012, 07:57:52 AM »
vl-member-if
vl-member-if-not

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Possible to return from middle of loop?
« Reply #11 on: February 06, 2012, 03:46:17 AM »

anybody home ??
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.

SIDESHOWBOB

  • Guest
Re: Possible to return from middle of loop?
« Reply #12 on: February 06, 2012, 05:37:41 AM »
Good grief what have I started!  Surely this question has been done to death on stackoverflow somewhere.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Possible to return from middle of loop?
« Reply #13 on: February 06, 2012, 10:51:26 AM »
vl-member-if
vl-member-if-not
Good point! Though I'm guessing it would be similar in performance to using the vl-some.

Good grief what have I started!  Surely this question has been done to death on stackoverflow somewhere.
Probably  :D ... I did go a bit overboard on this, sorry if it was irritating  :pissed: , I just liked running through the different methods - seeing how theory & practise would stack up with one another.

Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.