SlideShare a Scribd company logo
 Loops in Lisp
overviewParsing loop clausesLoop syntaxLoop constructsIteration controlConditional and unconditional executionData types
A loop is a series of expressions that are executed one or more times, a process known as iteration.The driving element of the loop facility is the loop macro.The expanded form consists of three basic parts in the tag body:Loop prologue contains forma that are executed before execution begins.
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.Parsing loop clausesSyntactic part of the loop construct are called clauses.The scope of each clause is determined by the top level parsing of that clause’s keyword.Example for a loop with six clauses:(loop for I from 1 to (compute-to-value)    while (not (unacceptable i))    collect (square i)    do (format t “Working on ~D now” i)    when (evenp i)      do (format t “ ~D is a non-odd number” i)finally (format t “About to exit!”))
Order of executionClauses are executed in the loop body in the order they appear in the sourceExecution is repeated until a clause terminates a loop or until the CL return, go or throw form is encountered.All variables are initialized first regardless of where the establishing clauses appear in the sourceThe code for any initially clauses is collected into one progn in the order in which the clauses appear in the source.The code for any finally clauses is collected into one progn in the order in which the clauses appear in the source.
A with clause introduces variable binding and an optional initial value.Iteration control clauses implicitly perform the following actions:Initializing variables
Stepping variables, generally between each execution of the loop body.
Performing termination tests, generally just before the execution of the loop body.Kinds of loopsVariable initialization and stepping:The for and as constructs provide iteration control clauses that establish a variable to be initialized.
The with construct is similar to a single let construct.
The repeat construct causes the iteration to terminate after a specified number of times.Value accumulation:The collect construct takes one form in its clause and adds the value of that form to the end of a list of values.
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.Termination conditionsLoop-finish lisp macro terminates iteration and returns any accumulated result.For and as constructs provides termination test that is determined by the iteration control clause.The repeat number causes termination after a specified number of iterations.While construct takes one form, a condition,  and terminates the iteration if the condition evaluates to a nil.The until construct is the inverse of while. It terminates.
The always construct takes one form and terminates the loop if the form ever evaluates to nil.The never construct takes one form and terminates the loop if the form ever evaluates to non-nillThe thereis construct takes one form and terminates the loop if the form ever evaluates to a non-nill.Unconditional execution:The do construct simply evaluates all forms in its clause.The return construct takes one form and returns its value.
Conditional execution:If construct one form as a predicate and a clause that is executed when the predicate is true.The when construct is a synonym for fi.The else construct provides an optional component of if, when and unless clauses that is executed when the prediction is false.The end construct provides an optional component to mark the end of a conditional clause.
Miscellaneous operationsThe named construct assigns a name to a loop construct.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.The finally construct causes its forms to be evaluated in the loop epilogue after normal iteration terminates.
Loop syntaxA loop consists of following type of clauses:Initial-final ::=initially|finallyVariables ::=with|initial-final|for-as|repeatMain ::=unconditional|accumulation|conditional|termination|initial-finalLoop ::=(loop [named name] {variables}* {main}* )
(loop {tag|expr}* )Where expr is any CL expression that can be evaluated , and tag  is any symbol not identifiable as a loop keyword.(loop do {tag|expr}*)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.
Examples for loops:;;; To print some numbers(loop as i from 1 to 4         do (print i))1     2    3    4NIL;;; To print every third number(loop for i from 10 downto 1 by 3             do (print i))10      7      4      1 NIL
;;; step incrementally from the default starting value(loop as i below 5              do (print i))0    1    2    3    4   NIL
;;; to print every other item in a list.(loop for item in ‘(1 2 3 4 5) bt #’ cddr               do (print item))1   3   5 NIL;;; collect successive tails of a list(loop for sublist on ‘(a b c d)          collect sublist)((A B C D)  (B C D) (C D) (D));;; print a list by using destructing with loop keyword ON.(loop for (item) on ‘(1 2 3)              do (print item))1     2    3  NIL;;; print items in a list without using destructing(loop for item in ‘(1 2 3)        do (print item))1    2    3 NIL
End-test controlThe loop keywords always, never, thereis, until, and while designate constructs that use a single test condition to determine when loop loop iteration should terminate.while expr while construct allows iteration to continue until the specified expression expr evaluates to nil.until expr until construct is equivalent to while, if the value of the specified expression is non-nil, iteration terminates.always expralways takes one form and terminates the loop if the form ever evaluates to nil.

More Related Content

What's hot (18)

Control flow statements in java
Control flow statements in javaControl flow statements in java
Control flow statements in java
yugandhar vadlamudi
 
Loops
LoopsLoops
Loops
Kulachi Hansraj Model School Ashok Vihar
 
Types of loops in c language
Types of loops in c languageTypes of loops in c language
Types of loops in c language
sneha2494
 
C# Loops
C# LoopsC# Loops
C# Loops
Hock Leng PUAH
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
Indraprastha Institute of Information Technology
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
Abhishek Choksi
 
C programming language tutorial
C programming language tutorialC programming language tutorial
C programming language tutorial
Dr. SURBHI SAROHA
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
Hossain Md Shakhawat
 
Loops in C
Loops in CLoops in C
Loops in C
Kamal Acharya
 
Template classes and ROS messages
Template classes and ROS messagesTemplate classes and ROS messages
Template classes and ROS messages
University of Colorado at Boulder
 
Looping (Computer programming and utilization)
Looping (Computer programming and utilization)Looping (Computer programming and utilization)
Looping (Computer programming and utilization)
Digvijaysinh Gohil
 
What is to loop in c++
What is to loop in c++What is to loop in c++
What is to loop in c++
03446940736
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
Jd Mercado
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)
Adam Mukharil Bachtiar
 
C# loops
C# loopsC# loops
C# loops
baabtra.com - No. 1 supplier of quality freshers
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
Sowmya Jyothi
 
Algorithm and Programming (Looping Structure)
Algorithm and Programming (Looping Structure)Algorithm and Programming (Looping Structure)
Algorithm and Programming (Looping Structure)
Adam Mukharil Bachtiar
 
Ch6 Loops
Ch6 LoopsCh6 Loops
Ch6 Loops
SzeChingChen
 

Similar to LISP: Loops In Lisp (20)

Looping statements
Looping statementsLooping statements
Looping statements
Jaya Kumari
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
LISP:Control Structures In Lisp
LISP:Control Structures In LispLISP:Control Structures In Lisp
LISP:Control Structures In Lisp
DataminingTools Inc
 
LISP: Control Structures In Lisp
LISP: Control Structures In LispLISP: Control Structures In Lisp
LISP: Control Structures In Lisp
LISP Content
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
Loops
LoopsLoops
Loops
Mohammed Alhafyan
 
Iteration
IterationIteration
Iteration
Liam Dunphy
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
06 Loops
06 Loops06 Loops
06 Loops
maznabili
 
pds first unit module 2 MODULE FOR ppt.pptx
pds first unit module 2 MODULE FOR ppt.pptxpds first unit module 2 MODULE FOR ppt.pptx
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
 
06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
kamalsmail1
 
Chapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdfChapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdf
AamirShahzad527024
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
Emroz Sardar
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
rnkhan
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
rnkhan
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Vb script final pari
Vb script final pariVb script final pari
Vb script final pari
Kamesh Shekhar Prasad
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
Loops c++
Loops c++Loops c++
Loops c++
Shivani Singh
 
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and loopingManaging input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
 
Looping statements
Looping statementsLooping statements
Looping statements
Jaya Kumari
 
LISP: Control Structures In Lisp
LISP: Control Structures In LispLISP: Control Structures In Lisp
LISP: Control Structures In Lisp
LISP Content
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
Ahmad Idrees
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
Eng Teong Cheah
 
pds first unit module 2 MODULE FOR ppt.pptx
pds first unit module 2 MODULE FOR ppt.pptxpds first unit module 2 MODULE FOR ppt.pptx
pds first unit module 2 MODULE FOR ppt.pptx
bmit1
 
06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx06-Control-Statementskkkkkkkkkkkkkk.pptx
06-Control-Statementskkkkkkkkkkkkkk.pptx
kamalsmail1
 
Chapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdfChapter 12 Computer Science ( ICS 12).pdf
Chapter 12 Computer Science ( ICS 12).pdf
AamirShahzad527024
 
Loop in C Properties & Applications
Loop in C Properties & ApplicationsLoop in C Properties & Applications
Loop in C Properties & Applications
Emroz Sardar
 
C# language basics (Visual Studio)
C# language basics (Visual Studio) C# language basics (Visual Studio)
C# language basics (Visual Studio)
rnkhan
 
C# language basics (Visual studio)
C# language basics (Visual studio)C# language basics (Visual studio)
C# language basics (Visual studio)
rnkhan
 
Constructs (Programming Methodology)
Constructs (Programming Methodology)Constructs (Programming Methodology)
Constructs (Programming Methodology)
Jyoti Bhardwaj
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
Neeru Mittal
 
Managing input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and loopingManaging input and output operations & Decision making and branching and looping
Managing input and output operations & Decision making and branching and looping
letheyabala
 

More from LISP Content (12)

LISP:Declarations In Lisp
LISP:Declarations In LispLISP:Declarations In Lisp
LISP:Declarations In Lisp
LISP Content
 
LISP: Errors In Lisp
LISP: Errors In LispLISP: Errors In Lisp
LISP: Errors In Lisp
LISP Content
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
LISP Content
 
LISP: Object Sytstem Lisp
LISP: Object Sytstem LispLISP: Object Sytstem Lisp
LISP: Object Sytstem Lisp
LISP Content
 
LISP: Type specifiers in lisp
LISP: Type specifiers in lispLISP: Type specifiers in lisp
LISP: Type specifiers in lisp
LISP Content
 
LISP: Symbols and packages in lisp
LISP: Symbols and packages in lispLISP: Symbols and packages in lisp
LISP: Symbols and packages in lisp
LISP Content
 
LISP: Scope and extent in lisp
LISP: Scope and extent in lispLISP: Scope and extent in lisp
LISP: Scope and extent in lisp
LISP Content
 
LISP: Program structure in lisp
LISP: Program structure in lispLISP: Program structure in lisp
LISP: Program structure in lisp
LISP Content
 
LISP: Predicates in lisp
LISP: Predicates in lispLISP: Predicates in lisp
LISP: Predicates in lisp
LISP Content
 
LISP: Macros in lisp
LISP: Macros in lispLISP: Macros in lisp
LISP: Macros in lisp
LISP Content
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
LISP Content
 
LISP: Introduction To Lisp
LISP: Introduction To LispLISP: Introduction To Lisp
LISP: Introduction To Lisp
LISP Content
 
LISP:Declarations In Lisp
LISP:Declarations In LispLISP:Declarations In Lisp
LISP:Declarations In Lisp
LISP Content
 
LISP: Errors In Lisp
LISP: Errors In LispLISP: Errors In Lisp
LISP: Errors In Lisp
LISP Content
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
LISP Content
 
LISP: Object Sytstem Lisp
LISP: Object Sytstem LispLISP: Object Sytstem Lisp
LISP: Object Sytstem Lisp
LISP Content
 
LISP: Type specifiers in lisp
LISP: Type specifiers in lispLISP: Type specifiers in lisp
LISP: Type specifiers in lisp
LISP Content
 
LISP: Symbols and packages in lisp
LISP: Symbols and packages in lispLISP: Symbols and packages in lisp
LISP: Symbols and packages in lisp
LISP Content
 
LISP: Scope and extent in lisp
LISP: Scope and extent in lispLISP: Scope and extent in lisp
LISP: Scope and extent in lisp
LISP Content
 
LISP: Program structure in lisp
LISP: Program structure in lispLISP: Program structure in lisp
LISP: Program structure in lisp
LISP Content
 
LISP: Predicates in lisp
LISP: Predicates in lispLISP: Predicates in lisp
LISP: Predicates in lisp
LISP Content
 
LISP: Macros in lisp
LISP: Macros in lispLISP: Macros in lisp
LISP: Macros in lisp
LISP Content
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
LISP Content
 
LISP: Introduction To Lisp
LISP: Introduction To LispLISP: Introduction To Lisp
LISP: Introduction To Lisp
LISP Content
 

Recently uploaded (20)

Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
MEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptxMEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptx
IC substrate Shawn Wang
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Cybersecurity Tools and Technologies - Microsoft Certificate
Cybersecurity Tools and Technologies - Microsoft CertificateCybersecurity Tools and Technologies - Microsoft Certificate
Cybersecurity Tools and Technologies - Microsoft Certificate
VICTOR MAESTRE RAMIREZ
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
How to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and TrendsHow to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and Trends
Nascenture
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
ACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentationACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentation
DanielEriksen5
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Gary Arora
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptxUiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
anabulhac
 
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdfKit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Kit-Works Team Study_팀스터디_김한솔_nuqs_20250509.pdf
Wonjun Hwang
 
MEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptxMEMS IC Substrate Technologies Guide 2025.pptx
MEMS IC Substrate Technologies Guide 2025.pptx
IC substrate Shawn Wang
 
Unlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web AppsUnlocking Generative AI in your Web Apps
Unlocking Generative AI in your Web Apps
Maximiliano Firtman
 
May Patch Tuesday
May Patch TuesdayMay Patch Tuesday
May Patch Tuesday
Ivanti
 
IT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information TechnologyIT488 Wireless Sensor Networks_Information Technology
IT488 Wireless Sensor Networks_Information Technology
SHEHABALYAMANI
 
Cybersecurity Tools and Technologies - Microsoft Certificate
Cybersecurity Tools and Technologies - Microsoft CertificateCybersecurity Tools and Technologies - Microsoft Certificate
Cybersecurity Tools and Technologies - Microsoft Certificate
VICTOR MAESTRE RAMIREZ
 
Building the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdfBuilding the Customer Identity Community, Together.pdf
Building the Customer Identity Community, Together.pdf
Cheryl Hung
 
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Digital Technologies for Culture, Arts and Heritage: Insights from Interdisci...
Vasileios Komianos
 
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Limecraft Webinar - 2025.3 release, featuring Content Delivery, Graphic Conte...
Maarten Verwaest
 
How to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and TrendsHow to Build an AI-Powered App: Tools, Techniques, and Trends
How to Build an AI-Powered App: Tools, Techniques, and Trends
Nascenture
 
Developing System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptxDeveloping System Infrastructure Design Plan.pptx
Developing System Infrastructure Design Plan.pptx
wondimagegndesta
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier VroomAI x Accessibility UXPA by Stew Smith and Olivier Vroom
AI x Accessibility UXPA by Stew Smith and Olivier Vroom
UXPA Boston
 
ACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentationACE Aarhus - Team'25 wrap-up presentation
ACE Aarhus - Team'25 wrap-up presentation
DanielEriksen5
 
fennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solutionfennec fox optimization algorithm for optimal solution
fennec fox optimization algorithm for optimal solution
shallal2
 
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptxReimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
Reimagine How You and Your Team Work with Microsoft 365 Copilot.pptx
John Moore
 
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Harmonizing Multi-Agent Intelligence | Open Data Science Conference | Gary Ar...
Gary Arora
 
Top-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptxTop-AI-Based-Tools-for-Game-Developers (1).pptx
Top-AI-Based-Tools-for-Game-Developers (1).pptx
BR Softech
 
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptxUiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
UiPath AgentHack - Build the AI agents of tomorrow_Enablement 1.pptx
anabulhac
 

LISP: Loops In Lisp

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