Debugging in LISP. trace causes a trace to be printed for a function when it is called

Similar documents
Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

INF4820. Common Lisp: Closures and Macros

Recursion & Iteration

Allegro CL Certification Program

Common LISP Tutorial 1 (Basic)

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

A little bit of Lisp

Modern Programming Languages. Lecture LISP Programming Language An Introduction

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Common Lisp. Blake McBride

Vanilla Lisp Shell (VLS)

Lecture Notes on Lisp A Brief Introduction

Department of Computer and information Science Norwegian University of Science and Technology

Artificial Intelligence Programming

Symbolic Computation and Common Lisp

(defvar *state* nil "The current state: a list of conditions.")

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

CSCI 2210: Programming in Lisp. Progn. Block. CSCI Programming in Lisp; Instructor: Alok Mehta 1. ANSI Common Lisp, Chapters 5-10

Functional Languages. Hwansoo Han

Introduction to Lisp

Documentation for LISP in BASIC

Allegro CL Certification Program

A Quick Introduction to Common Lisp

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM

Lecture #2 Kenneth W. Flynn RPI CS

Structure Programming in Lisp. Shared Structure. Tailp. Shared Structure. Top-Level List Structure

CS 360 Programming Languages Interpreters

Basics of Using Lisp! Gunnar Gotshalks! BLU-1

Allegro CL Certification Program

Recap: Functions as first-class values

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Chapter 1. Fundamentals of Higher Order Programming

CSE 413 Midterm, May 6, 2011 Sample Solution Page 1 of 8

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

GDB QUICK REFERENCE GDB Version 4

Robot Programming with Lisp

Lisp Basic Example Test Questions

UNIT 3

AllegroCache: an Introduction

Programming Languages

Robot Programming with Lisp

An Explicit Continuation Evaluator for Scheme

Attributes of Variable. Lecture 13: Run-Time Storage Management. Lifetime. Value & Location

Artificial Intelligence. Section Notes: Week 1 September 2, 2010

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

CSCC24 Functional Programming Scheme Part 2

Princeton University COS 217: Introduction to Programming Systems GDB Tutorial and Reference

A Brief Introduction to Common Lisp

Functional programming with Common Lisp

ALISP interpreter in Awk

Introduction to Functional Programming and basic Lisp

CSCI337 Organisation of Programming Languages LISP

Programming Languages

History. Functional Programming. Based on Prof. Gotshalks notes on functionals. FP form. Meaningful Units of Work

Announcements. Today s Menu

Common LISP-Introduction

Begin at the beginning

Names, Bindings, Scopes

LAB WORK NO. 3 TURBO DEBUGGER ENVIRONMENT

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

University of Massachusetts Lowell

Erlangen API Documentation

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

Meet the Macro. a quick introduction to Lisp macros. Patrick Stein / TC Lisp Users Group /

Functional Programming. Pure Functional Programming

SCHEME AND CALCULATOR 5b

Introduction 2 Lisp Part III

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Parsing Scheme (+ (* 2 3) 1) * 1

SOFTWARE ARCHITECTURE 6. LISP

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Nano-Lisp The Tutorial Handbook

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Using the Debugger. Michael Jantz Dr. Prasad Kulkarni

Lecture 8: Recursion and Iteration. Exceptions. Declarative Programming.

Lambda Calculus see notes on Lambda Calculus

Example. program sort; var a : array[0..10] of integer; procedure readarray; : function partition (y, z :integer) :integer; var i, j,x, v :integer; :

CL1 Manual. Alan Bawden ABSTRACT

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Functions, Conditionals & Predicates

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

So what does studying PL buy me?

An introduction to Scheme

Imperative, OO and Functional Languages A C program is

Functional Programming. Pure Functional Languages

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

A Fast Review of C Essentials Part I

Principles of Programming Languages 2017W, Functional Programming

Useful Unix Commands Cheat Sheet

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Transcription:

trace causes a trace to be printed for a function when it is called ;;; a function that works like reverse (defun rev (list) (cons (first (last list)) (rev (butlast list)))) USER: (trace rev) ; note trace is a macro (REV) USER: (rev (a b c d e)) 0: (REV (A B C D E)) 1: (REV (A B C D)) 2: (REV (A B C)) 3: (REV (A B)) 4: (REV (A)) 5: (REV NIL) 6: (REV NIL) 7: (REV NIL) 8: (REV NIL) 9: (REV NIL) 10: (REV NIL)...... Page 1 of 14

tracing ;;; a debugged function that works like reverse (defun rev (list) (if (endp list) NIL (cons (first (last list)) (rev (butlast list))))) USER: (rev (a b c d e)) 0: (REV (A B C D E)) 1: (REV (A B C D)) 2: (REV (A B C)) 3: (REV (A B)) 4: (REV (A)) 5: (REV NIL) 5: returned NIL 4: returned (A) 3: returned (B A) 2: returned (C B A) 1: returned (D C B A) 0: returned (E D C B A) (E D C B A) Page 2 of 14

several functions can be traced at once (defun rev (list) (rev-aux list nil)) (defun rev-aux (list acc) (if (endp list) acc (rev-aux (rest list) (cons (first list) acc)))) USER: (trace rev-aux) (REV-AUX) USER: (trace) ;with no args, trace lists all traced fns (REV REV-AUX) USER: (rev (a b c)) 0: (REV (A B C)) 1: (REV-AUX (A B C) NIL) 2: (REV-AUX (B C) (A)) 3: (REV-AUX (C) (B A)) 4: (REV-AUX NIL (C B A)) 4: returned (C B A) 3: returned (C B A) 2: returned (C B A) 1: returned (C B A) 0: returned (C B A) (C B A) Page 3 of 14

A note on efficiency rev-aux is an example of a tail-recursive function A tail recursive function calls itself and returns the result without doing any additional computation on it The trace of rev-aux shows that many stack operations could be eliminated because of this A clever interpreter or compiler can easily translate a tail-recursive function into an iterative loop for efficiency Writing tail-recursive aux -type functions with an additional accumlator argument can lead to more efficient (though perhaps less elegant) programs Another example: ; a function that works like length (not tail recursive) (defun len (list) (if (endp list) 0 (+ 1 (len (rest list))))) ; a function that works like length (defun len (list) (len-aux list 0)) ; len-aux is tail recursive (defun len-aux (list acc) (if (endp list) acc (len-aux (rest list) (+ 1 acc)))) Page 4 of 14

To stop tracing a particular function, use untrace USER: (untrace rev) (REV) To stop tracing all functions, use untrace without any arguments USER: (untrace) NIL USER: (trace) NIL Page 5 of 14

step causes the LISP evaluation process to be stepped through USER: (step (rev (a b c))) 1: (REV (A B C)) [step] USER: <cr> 2: (A B C) => (A B C) 2: (BLOCK REV (REV-AUX LIST NIL)) [step] USER: <cr> 3: (REV-AUX LIST NIL) [step] USER: <cr> 4: LIST => (A B C) 4: NIL => NIL 4: T => T 4: (AND (CONSP EXCL::X) (LET ((EXCL::X #)) (MEMBER EXCL:: X...))) [step] USER: :reset USER: step may not be very useful Page 6 of 14

break causes a computation to be interrupted at a particular point the value of local variables can be inspected during the break the runtime stack of function calls can be inspected during the break insert the expression (break message ) where you want the break to occur the message will be printed when the break occurs (defun rev-aux (list acc) (when (= (length list) 2) (break length is 2 )) (if (endp list) acc (rev-aux (rest list) (cons (first list) acc)))) Page 7 of 14

using break USER: (rev (a b c)) Break: length is 2 Restart actions (select using :continue): 0: return from break. [1c] USER: :local list (B C) [1c] USER: :local acc (A) [1c] USER: :zoom Evaluation stack: (BREAK length is 2 ) ->(PROGN (BREAK length is 2 )) (IF (= # 2) (PROGN #)...) (REV-AUX (B C) (A)) (IF (ENDP LIST) ACC...) (REV-AUX (A B C) NIL) (REV (A B C)) (EVAL (REV #)) (TPL:TOP-LEVEL-READ-EVAL-PRINT-LOOP) [1c] USER: :continue (C B A) Page 8 of 14

Many top-level commands are available in Allegro Common LISP :reset, :continue, :local, :zoom are the most commonly used Use :help to see them all USER: :help COMMAND ABBR DESCRIPTION aliases ali print all command aliases args arg save arguments before calls arrest arr arrest a process for debugging boe Mark frame to break when exitted. bottom bo Zoom at the oldest frame on the stack. bt Zoom in a very brief manner. cd change into another directory cf compile a file cload cl compile and load a file continue cont continue from a continuable error current cur return the expression given by the current stack frame dirs di print the Allegro directory stack dn move down the stack n frames, default 1 edit ed edit the source for the current stack frame EOF either :pop or :exit error err print the last error message evalmode eval examine or set evaluation mode exit ex exit and return to the shell find fin find the stack frame calling the function func focus fo focus the top level on a process frame fr print info about current frame......... Page 9 of 14

time is useful for checking efficiency of functions USER: (time (rev (a b c d e f g h i j k l m n o p))) cpu time (non-gc) 16 msec user, 0 msec system cpu time (gc) 0 msec user, 0 msec system cpu time (total) 16 msec user, 0 msec system real time 20 msec space allocation: 238 cons cells, 0 symbols, 608 other bytes, (P O N M L K J I H G...) describe is useful for finding information about LISP objects USER: (describe rev) REV is a SYMBOL It is unbound. It is INTERNAL in the COMMON-LISP-USER package. Its function binding is #<Interpreted Function REV @#0f2e> The function takes arguments (LIST) Page 10 of 14

Iteration in LISP A language with recursion and conditionals is computationally universal, but... Iteration is natural for some problems Iteration may be more efficient than recursion in some implementations Common LISP provides many iteration constructs Page 11 of 14

Iteration in LISP dotimes is used for counting-oriented iteration (dotimes (var count result) expr1 expr2...) var is a symbol that acts as an iteration variable count is evaluated; it should evaluate to an integer the expressions expr1, expr2,... in the body of the dotimes are each evaluated with var bound to integers from 0 (inclusive) to count (exclusive) result is evaluated and returned as the value of the dotimes result is optional; if missing, dotimes returns nil ;;; raise m to the n power ;;; m and n must be integers, n non-negative (defun dotimes-expt (m n) (let ((result 1)) (dotimes (count n result) (setf result (* m result))))) Page 12 of 14

Iteration in LISP dolist is useful for list-oriented iteration (dolist (var list result) expr1 expr2...) var is a symbol that acts as an iteration variable list is evaluated; it should evaluate to a list the expressions expr1, expr2,... in the body of the dolist are each evaluated with var bound to the elements of list, in order result is evaluated and returned as the value of the dolist result is optional; if missing, dolist returns nil ;;; return the length of a list (defun dolist-length (list) (let ((result 0)) (dolist (item list result) (setf result (+ 1 result))))) Page 13 of 14

Control strategies in LISP: Recursion vs. Mapping vs. Iteration In general: write code that is easy to understand, while meeting requirements for efficiency, if any When writing a mathematical function, if the definition of the function suggests a control strategy, then using that strategy probably makes sense If solving a problem requires looking at nested list structure, then recursion probably makes sense If solving a problem involves transforming a list into a new list, then mapping probably makes sense If solving a problem calls for doing something over and over (and over), then iteration probably makes sense Page 14 of 14