Your SlideShare is downloading. ×
0
AI Programming
             Week Two:
 Procedures, the Stack & Debugging
Richard Price
rmp@cs.bham.ac.uk
Overview
    Introduction
•
•   Recap
•   Procedures
•   The Stack




                   2
Course Details
http://www.cs.bham.ac.uk/internal/courses/ai-prog-a/
• Lab sessions:
   – Monday 3pm - 5pm.
   – Friday 2pm...
Recap
• Variables & Expressions
  – Print arrow
  – Semicolon
  – Declaration
  – Assignment
  – Data Types
  – Comments.
...
List
• Lists:
   [1 2 3] =>
   ** [1 2 3]

   lvars mylist = [19 18 25];
   mylist =>
   mylist(1) =>
   ** 19

   lvars l...
Procedures
• Programs can become very large.
• Procedures break down code into pieces.
  – Reusable.
  – Recognisable.
  –...
Built in procedures



• Addition and division are built in
  procedures.
• Other examples:
  max(2,5) =>
  ** 5
  min(2,5...
Write your own procedures
• Note the brackets in max(2,5);

  define myprocedure()
      ;;; code goes here
  enddefine;
 ...
Variables & Procedures
• lvars declares local variables.
  – Local to a procedure.




                                   ...
Arguments & Procedues
• Procedures often take in arguments.
  – +(oneNumber, anotherNumber);
  – max(argument1, argument2)...
Arguments & Procedures
• Write a procedure called addition that:
  – Takes in two numbers as arguments.
  – Adds them toge...
Returning results
• Procedures often return results:
  – Max returns the maximum of two numbers.

  define max(firstNumber...
Multiple Arguments & Results
• Procedures often return results:
  – Max returns the maximum of two numbers.

  define prin...
Procedures in Procedures

• Procedures can be used within
  procedures:
define minOfThree(number1, number2, number3) -> sm...
The Pop-11 Stack

• Almost everything in Pop-11 uses the
  stack.
• If variables are just containers.
• Then the stack is ...
Assignment and the Stack

• Assignment in Pop-11
  – Uses the stack!

  – lvars creates the x variable.
  – 4 is place on ...
17
Viewing the Stack
lvars myVariable;
5 -> myVariable;
myVariable =>




4;
5;
6;
=>
** 4 5 6

                    18
Procedures & the stack




3;
11;
max();
=>
                         19
Upcoming SlideShare
Loading in...5
×

Procedures, the Pop-11 stack and debugging

273

Published on

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
273
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Procedures, the Pop-11 stack and debugging"

  1. 1. AI Programming Week Two: Procedures, the Stack & Debugging Richard Price rmp@cs.bham.ac.uk
  2. 2. Overview Introduction • • Recap • Procedures • The Stack 2
  3. 3. Course Details http://www.cs.bham.ac.uk/internal/courses/ai-prog-a/ • Lab sessions: – Monday 3pm - 5pm. – Friday 2pm-4pm. • Assessment Deadline – Mondays 5pm. • Late submissions until: – Wednesday 10am. – 50% penalty. 3
  4. 4. Recap • Variables & Expressions – Print arrow – Semicolon – Declaration – Assignment – Data Types – Comments. 4
  5. 5. List • Lists: [1 2 3] => ** [1 2 3] lvars mylist = [19 18 25]; mylist => mylist(1) => ** 19 lvars listInAList = [1 [2 3]]; listInAList(2) => ** [2 3]; 5
  6. 6. Procedures • Programs can become very large. • Procedures break down code into pieces. – Reusable. – Recognisable. – Named mini-program. 6
  7. 7. Built in procedures • Addition and division are built in procedures. • Other examples: max(2,5) => ** 5 min(2,5) => ** 2 • In Xved, press Enter then type help max. 7
  8. 8. Write your own procedures • Note the brackets in max(2,5); define myprocedure() ;;; code goes here enddefine; myprocedure(); define printTwo() 2 => enddefine; printTwo(); ** 2 8
  9. 9. Variables & Procedures • lvars declares local variables. – Local to a procedure. 9
  10. 10. Arguments & Procedues • Procedures often take in arguments. – +(oneNumber, anotherNumber); – max(argument1, argument2); define printTwice(valueToPrint); valueToPrint => valueToPrint => enddefine; printTwice(‘Rich’); ** Rich ** Rich 10
  11. 11. Arguments & Procedures • Write a procedure called addition that: – Takes in two numbers as arguments. – Adds them together. – And prints the result. 11
  12. 12. Returning results • Procedures often return results: – Max returns the maximum of two numbers. define max(firstNumber, secondNumber) -> largerNumber; … enddefine; max(5,25) => ** 25 12
  13. 13. Multiple Arguments & Results • Procedures often return results: – Max returns the maximum of two numbers. define printAPlusBPlusC(a,b,c) a + b + c => enddefine; define returnAthenB(a,b) -> (result1, result2); a -> result1; b -> result2; enddefine; 13
  14. 14. Procedures in Procedures • Procedures can be used within procedures: define minOfThree(number1, number2, number3) -> smallestOfThree; lvars smallestOfTwo; min(number1, number2) -> smallestOfTwo; min(number3, smallestOfTwo) -> smallestOfThree; enddefine; 14
  15. 15. The Pop-11 Stack • Almost everything in Pop-11 uses the stack. • If variables are just containers. • Then the stack is a pile of boxes. 15
  16. 16. Assignment and the Stack • Assignment in Pop-11 – Uses the stack! – lvars creates the x variable. – 4 is place on the stack – Assignment pops 4 off. – Into the x variable. 16
  17. 17. 17
  18. 18. Viewing the Stack lvars myVariable; 5 -> myVariable; myVariable => 4; 5; 6; => ** 4 5 6 18
  19. 19. Procedures & the stack 3; 11; max(); => 19
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×