Loops in Lisp<br />
overview<br />Parsing loop clauses<br />Loop syntax<br />Loop constructs<br />Iteration control<br />Conditional and uncon...
A loop is a series of expressions that are executed one or more times, a process known as iteration.<br />The driving elem...
Loop body contains those forms that are executed during iteration.
Stepping is the process of assigning the variable the next item in the series of items.
Loop epilogue contains forms that are executed after iteration terminates.</li></li></ul><li>Parsing loop clauses<br />Syn...
Order of execution<br />Clauses are executed in the loop body in the order they appear in the source<br />Execution is rep...
A with clause introduces variable binding and an optional initial value.<br />Iteration control clauses implicitly perform...
Stepping variables, generally between each execution of the loop body.
Performing termination tests, generally just before the execution of the loop body.</li></li></ul><li>Kinds of loops<br />...
The with construct is similar to a single let construct.
The repeat construct causes the iteration to terminate after a specified number of times.</li></li></ul><li>Value accumula...
Append construct takes one form in its clause and appends the value of that form to the end of a list of values
The sum construct takes one form in its clause that must evaluate to a number and adds that number into a running total.
The count construct takes one form in its clause and counts the number of times that form evaluates to a non-nil value.
The minimize construct takes one form In clause and determines the minimum value obtained by evaluating the form.
The maximizeconstruct takes one form In clause and determines the maximum value obtained by evaluating the form.</li></li>...
The always construct takes one form and terminates the loop if the form ever evaluates to nil.<br />The never construct ta...
Conditional execution:<br />If construct one form as a predicate and a clause that is executed when the predicate is true....
Miscellaneous operations<br />The named construct assigns a name to a loop construct.<br />The initially construct causes ...
Loop syntax<br />A loop consists of following type of clauses:<br />Initial-final ::=initially|finally<br />Variables ::=w...
(loop {tag|expr}* )<br />Where expr is any CL expression that can be evaluated , and tag  is any symbol not identifiable a...
Examples for loops:<br />;;; To print some numbers<br />(loop as i from 1 to 4<br />         do (print i))<br />1<br />  ...
;;; step incrementally from the default starting value<br />(loop as i below 5<br />              do (print i))<br />0<br...
;;; to print every other item in a list.<br />(loop for item in ‘(1 2 3 4 5) bt #’ cddr<br />               do (print item...
End-test control<br />The loop keywords always, never, thereis, until, and while designate constructs that use a single te...
Upcoming SlideShare
Loading in …5
×

LISP: Loops In Lisp

1,431 views

Published on

LISP: Loops In Lisp

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,431
On SlideShare
0
From Embeds
0
Number of Embeds
18
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

LISP: Loops In Lisp

  1. 1. Loops in Lisp<br />
  2. 2. overview<br />Parsing loop clauses<br />Loop syntax<br />Loop constructs<br />Iteration control<br />Conditional and unconditional execution<br />Data types<br />
  3. 3. A loop is a series of expressions that are executed one or more times, a process known as iteration.<br />The driving element of the loop facility is the loop macro.<br />The expanded form consists of three basic parts in the tag body:<br /><ul><li>Loop prologue contains forma that are executed before execution begins.
  4. 4. Loop body contains those forms that are executed during iteration.
  5. 5. Stepping is the process of assigning the variable the next item in the series of items.
  6. 6. Loop epilogue contains forms that are executed after iteration terminates.</li></li></ul><li>Parsing loop clauses<br />Syntactic part of the loop construct are called clauses.<br />The scope of each clause is determined by the top level parsing of that clause’s keyword.<br />Example for a loop with six clauses:<br />(loop for I from 1 to (compute-to-value)<br /> while (not (unacceptable i))<br /> collect (square i)<br /> do (format t “Working on ~D now” i)<br /> when (evenp i)<br /> do (format t “ ~D is a non-odd number” i)<br />finally (format t “About to exit!”))<br />
  7. 7. Order of execution<br />Clauses are executed in the loop body in the order they appear in the source<br />Execution is repeated until a clause terminates a loop or until the CL return, go or throw form is encountered.<br />All variables are initialized first regardless of where the establishing clauses appear in the source<br />The code for any initially clauses is collected into one progn in the order in which the clauses appear in the source.<br />The code for any finally clauses is collected into one progn in the order in which the clauses appear in the source.<br />
  8. 8. A with clause introduces variable binding and an optional initial value.<br />Iteration control clauses implicitly perform the following actions:<br /><ul><li>Initializing variables
  9. 9. Stepping variables, generally between each execution of the loop body.
  10. 10. Performing termination tests, generally just before the execution of the loop body.</li></li></ul><li>Kinds of loops<br />Variable initialization and stepping:<br /><ul><li>The for and as constructs provide iteration control clauses that establish a variable to be initialized.
  11. 11. The with construct is similar to a single let construct.
  12. 12. The repeat construct causes the iteration to terminate after a specified number of times.</li></li></ul><li>Value accumulation:<br /><ul><li>The collect construct takes one form in its clause and adds the value of that form to the end of a list of values.
  13. 13. Append construct takes one form in its clause and appends the value of that form to the end of a list of values
  14. 14. The sum construct takes one form in its clause that must evaluate to a number and adds that number into a running total.
  15. 15. The count construct takes one form in its clause and counts the number of times that form evaluates to a non-nil value.
  16. 16. The minimize construct takes one form In clause and determines the minimum value obtained by evaluating the form.
  17. 17. The maximizeconstruct takes one form In clause and determines the maximum value obtained by evaluating the form.</li></li></ul><li>Termination conditions<br />Loop-finish lisp macro terminates iteration and returns any accumulated result.<br />For and as constructs provides termination test that is determined by the iteration control clause.<br />The repeat number causes termination after a specified number of iterations.<br />While construct takes one form, a condition, and terminates the iteration if the condition evaluates to a nil.<br />The until construct is the inverse of while. It terminates.<br />
  18. 18. The always construct takes one form and terminates the loop if the form ever evaluates to nil.<br />The never construct takes one form and terminates the loop if the form ever evaluates to non-nill<br />The thereis construct takes one form and terminates the loop if the form ever evaluates to a non-nill.<br />Unconditional execution:<br />The do construct simply evaluates all forms in its clause.<br />The return construct takes one form and returns its value.<br />
  19. 19. Conditional execution:<br />If construct one form as a predicate and a clause that is executed when the predicate is true.<br />The when construct is a synonym for fi.<br />The else construct provides an optional component of if, when and unless clauses that is executed when the prediction is false.<br />The end construct provides an optional component to mark the end of a conditional clause.<br />
  20. 20. Miscellaneous operations<br />The named construct assigns a name to a loop construct.<br />The initially construct causes its forms to be evaluated in the loop prologue, which proceeds all loop code except for initial settings specified by the constructs with, for, or as.<br />The finally construct causes its forms to be evaluated in the loop epilogue after normal iteration terminates.<br />
  21. 21. Loop syntax<br />A loop consists of following type of clauses:<br />Initial-final ::=initially|finally<br />Variables ::=with|initial-final|for-as|repeat<br />Main ::=unconditional|accumulation<br />|conditional|termination|initial-final<br />Loop ::=(loop [named name] {variables}* {main}* )<br />
  22. 22. (loop {tag|expr}* )<br />Where expr is any CL expression that can be evaluated , and tag is any symbol not identifiable as a loop keyword.<br />(loop do {tag|expr}*)<br />A loop prologue consists of any automatic variable initializations prescribed by the variable clauses, along with any initially clauses in the order they appear in the source.<br />
  23. 23. Examples for loops:<br />;;; To print some numbers<br />(loop as i from 1 to 4<br /> do (print i))<br />1<br /> 2<br /> 3<br /> 4<br />NIL<br />;;; To print every third number<br />(loop for i from 10 downto 1 by 3 <br /> do (print i))<br />10<br /> 7<br /> 4<br /> 1<br /> NIL<br />
  24. 24. ;;; step incrementally from the default starting value<br />(loop as i below 5<br /> do (print i))<br />0<br /> 1<br /> 2<br /> 3<br /> 4<br /> NIL<br />
  25. 25. ;;; to print every other item in a list.<br />(loop for item in ‘(1 2 3 4 5) bt #’ cddr<br /> do (print item))<br />1<br /> 3<br /> 5<br /> NIL<br />;;; collect successive tails of a list<br />(loop for sublist on ‘(a b c d)<br /> collect sublist)<br /><ul><li>((A B C D) (B C D) (C D) (D))</li></li></ul><li>;;; print a list by using destructing with loop keyword ON.<br />(loop for (item) on ‘(1 2 3)<br /> do (print item))<br /><ul><li>1</li></ul> 2<br /> 3<br /> NIL<br />;;; print items in a list without using destructing<br />(loop for item in ‘(1 2 3)<br /> do (print item))<br /><ul><li>1</li></ul> 2<br /> 3<br /> NIL<br />
  26. 26. End-test control<br />The loop keywords always, never, thereis, until, and while designate constructs that use a single test condition to determine when loop loop iteration should terminate.<br />while expr while construct allows iteration to continue until the specified expression expr evaluates to nil.<br />until expr until construct is equivalent to while, if the value of the specified expression is non-nil, iteration terminates.<br />always expralways takes one form and terminates the loop if the form ever evaluates to nil. <br />
  27. 27. Never expr never form takes one form and terminates the loop if the form ever evaluates to non-nill.<br />thereis expr thereis construct takes one form and terminates the loop if the form ever evaluates to a non-nil.<br />While and until constructs causes termination, control is passed to the loop epilogue, where any finally clauses will be executed.<br />
  28. 28. Ex:<br />(loop for i from 0 to 10<br /> always ( < i 11)) T<br />(loop for i from 0 to 10<br /> never (> i 11)) T<br />(loop for i from 0 to 10<br /> always ( < i 9)<br /> finally (print “you won’t see this”))NIL<br />(loop never t<br /> finally (print “you won’t see this”))NIL<br />
  29. 29. (loop thereis “Here is my value”<br /> finally (print “you won’t see this”)) “here is my value”<br />(loop for i from 0 to 10<br /> thereis (when (> i 10) i) ) 11<br />(loop for i from 1 to 10<br /> thereis (> i 11)<br /> finally (print i)) 11<br /> NIL<br />
  30. 30. Unconditional execution<br />The loop construct do takes one or more expressions and simply evaluates them in order.<br />do {expr} do construct simply evaluates the specified expressions wherever they occur in the expanded form of loop.<br />Ex: (loop for ifrom 1 to 5<br /> do (print i))<br /><ul><li>1</li></ul> 2<br /> 3<br /> 4<br /> 5<br />NIL.<br />
  31. 31. return expr return construct terminates the loop and returns the value of the specified expression as the value of the loop .<br />Ex: (loop for item in ‘( 1 2 3 a 4 5)<br /> when (not (numberp item))<br /> return (cerror “enter new value” <br /> “non-numeric value: ~s”<br /> item))<br /> Error: non-numeric value: A<br />
  32. 32. Data types<br />type-spec argument that allows you to specify certain data types for loop variables.<br />The type-spec argument syntax:<br />type-spec ::=of-type d-type-spec<br />d-type-spec ::=type-specifier|(d-type-spec . D-type-spec)<br />The type-spec argument is used for destructing<br />
  33. 33. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at www.dataminingtools.net<br />

×