Jump to content

"while" loop Misunderstanding


KrazyMann225

Recommended Posts

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 by KrazyMann225
I can't spell or proof read
Link to comment
Share on other sites

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 by BlackBox
Non-NIL
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)
)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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))
)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...