KrazyMann225 Posted July 20, 2011 Share Posted July 20, 2011 (edited) I wanted my routine to search a file and a found the necessary information to do it, and it works.....but I don't know how. I was wondering if anyone to explain to me how. The part that I don't understand is: (while (setq txtLine(read-line f)) my question is, while what. I'm pretty new at this so I'm sure it's just something I don't know/haven't learnt yet but shouldn't there be an expression to evaluate? Or is it saying "until txtline = nil" essentially? Edited July 20, 2011 by KrazyMann225 I can't spell or proof read Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 20, 2011 Share Posted July 20, 2011 (edited) Good to see you again... let me take a crack at clarifying the while function for you. So long as the TestExpression tests as Non-NIL, the subsequent expression(s) will be executed. Once the TestExpression tests as NIL, the While loop is complete and the code moves on to the next line. Syntax: (while [color=red]<TestExpression>[/color] [color=blue]<Expression>[/color] ) Using your code example.... (while [color=red](setq txtLine(read-line f))[/color] [color=blue]<Expression>[/color] ) Edit: So what your code is doing, is "while" variable txtLine has a value (as a result of the next line being read from the file), something esle is done. This will continue until the end of the file is reached where there are no more lines to read. For reference: Evaluates a test expression, and if it is not nil, evaluates other expressions; repeats this process until the test expression evaluates to nil (while testexpr [expr...]) The while function continues until testexpr is nil. Arguments testexpr The expression containing the test condition. expr One or more expressions to be evaluated until testexpr is nil. Return Values The most recent value of the last expr. Edited July 20, 2011 by BlackBox Non-NIL Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 20, 2011 Share Posted July 20, 2011 So long as the TestExpression tests as T (True), the subsequent expression will be executed. Once the TextExpression tests as NIL, the While loop is complete and the code moves on to the next line. Aside: Test Expression returns [font=Georgia][i]nil [/i][/font] [b][color=green]==>[/color][/b] [i][font=Georgia]while [/font][/i]loop terminates [font=Georgia][i]while [/i][/font]loop continues[color=red] [b]=/=>[/b][/color] Test Expression returned [font=Georgia][i]T[/i][/font] Since any non-nil return from the test expression will permit the while loop to continue evaluation. /nitpick Quote Link to comment Share on other sites More sharing options...
BlackBox Posted July 20, 2011 Share Posted July 20, 2011 /nitpick Deserved. Thanks for the clarification, Lee. :wink: Quote Link to comment Share on other sites More sharing options...
Jeff H Posted July 20, 2011 Share Posted July 20, 2011 Hey Lee or RenderMan, Out of curiosity does Lisp have the equivalent of a Do While loop The While loop does the test and if T then executes the code. The Do While executes the expressions at least once For example: (Do [Expr] While testexpr) So executes the Expr and keeps executing the Expr as long as testexpr evaluates to T Thanks guys Quote Link to comment Share on other sites More sharing options...
KrazyMann225 Posted July 21, 2011 Author Share Posted July 21, 2011 That makes sense. No matter how many times I read the help file over at work I couldn't get it straight. I kept thinking that you needed to "evaluate" something (thinking only =, /=, ) Lee Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 21, 2011 Share Posted July 21, 2011 Out of curiosity does Lisp have the equivalent of a Do While loop The While loop does the test and if T then executes the code. The Do While executes the expressions at least once For example: (Do [Expr] While testexpr) So executes the Expr and keeps executing the Expr as long as testexpr evaluates to T Surprisingly, LISP doesn't have such a construct, but that's not to say you can't roll your own: (defun DoWhile ( expr test / flag ) (while (not flag) (Expr) (if (not (test)) (setq flag t)) ) ) Eg. (defun c:test ( / i ) (setq i 0) (DoWhile (lambda ( ) (print (setq i (1+ i)))) (lambda ( ) (< i 10)) ) (princ) ) Quote Link to comment Share on other sites More sharing options...
irneb Posted July 21, 2011 Share Posted July 21, 2011 The Do While executes the expressions at least once For example: (Do [Expr] While testexpr) So executes the Expr and keeps executing the Expr as long as testexpr evaluates to T Thanks guys Not in AutoLisp/VisualLisp. Some of the other flavours of Lisp (like Common Lisp / Clojure / etc.): http://rosettacode.org/wiki/Loops/Do-while#Common_Lisp BTW, I still feel that Delphi/Pascal had the "best" naming for this type of loop (most descriptive/understandable anyway): Repeat [Expressions] Until [Test] Anyhow, you can implement something similar into AutoLisp, but it's not going to work as smoothly. E.g.: (defun doWhile (expr test / ) (eval expr) (while (eval test) (eval expr)) ) Then you need to call it using some quoted portions, e.g.: (doWhile '(progn (print i) (setq i (1+ i)) ) '(< i 10) ) Edit: Dam! Sorry didn't see this Lee. Anyhow, mine is slightly different though - no need for those external lambdas! Quote Link to comment Share on other sites More sharing options...
Jeff H Posted July 21, 2011 Share Posted July 21, 2011 Thanks guys, VB uses sorto of the same Do While and Do Until http://msdn.microsoft.com/en-us/library/eked04a7.aspx#Y525 Quote Link to comment Share on other sites More sharing options...
Lee Mac Posted July 21, 2011 Share Posted July 21, 2011 Edit: Dam! Sorry didn't see this Lee. Anyhow, mine is slightly different though - no need for those external lambdas! No worries I would be inclined to use the structure of your function (to avoid the extra variable), but still pass it lambda functions to avoid the multiple 'eval's (defun DoWhile ( expr test ) (expr) (while (test) (expr)) ) Quote Link to comment Share on other sites More sharing options...
irneb Posted July 21, 2011 Share Posted July 21, 2011 True! That would probably make for much more efficient code! It's sorry we're so limited with AutoLisp. This would have been a perfect defun if we were able to use indefinite length arguments. Then it would be possible to make the dowhile to work exactly similar to what the normal while does. Though if we didn't have ALisp's limitations, we'd not have needed to "make" a doWhile in the first place. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.