04 Control. Control-Blocks Common Lisp has 3 basic operators for creating blocks of code progn block tagbody If ordinary function calls are the leaves.

Slides:



Advertisements
Similar presentations
Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
Advertisements

09 Examples Functional Programming. Tower of Hanoi AB C.
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
ANSI Common Lisp 5. Control 16 June Blocks -- progn progn > (progn (format t “a”) (format t “b”) ( )) ab 23 The expressions within its body.
1 Programming Languages and Paradigms Lisp Programming.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Returning values from functions You can return a value from a function by using the built- in function : ( return-from Function_name value) For example:
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Allegro CL Certification Program Lisp Programming Series Level I Session Top Ten Things to Know.
Yu-Tzu Lin ( 林育慈 )
For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.
Functional Programming
Functional Programming 02 Lists
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
Control Structures We have already visited some of the control structures (if, if-else, dolist, dotimes) –Here we take a more in-depth look at control.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Iteration Chapters 6 & 7. Iteration in LISP n LISP (unlike Prolog) allows iteration –mapcar, remove-if(-not), count-if, find-if for special purpose iteration.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
PRACTICAL COMMON LISP Peter Seibel 1.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
1. switch statement 2. “Nested” statements Conditionals, part 2 1.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Lists CSC 358/ Outline Lab #1 / Homework #1 Lists A question List Internals of Lists List operations Extended Example.
Basic Introduction to Lisp
Control Structures CSC 358/ Outline Midterm Lab #3 Homework #4 Sequential structures Conditional structures Unconditional branching Iteration.
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
Functional Programming: Lisp MacLennan Chapter 10.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Section 15.4, 15.6 plus other materials
Example of formula (defun roots (a b c) (list
Getting Started with Lisp
Using Lisp Lisp is a interactive system
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Common Lisp II.
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Programming Languages
The general format of case is the following: (case <key form>
Presentation transcript:

04 Control

Control-Blocks Common Lisp has 3 basic operators for creating blocks of code progn block tagbody If ordinary function calls are the leaves of a Lisp program, these operators are used to build the branches

Control-Blocks > (progn (format t “a”) (format t “b”) ( ) ) ab 23 -> only the value of the last expression is returned

Control-Blocks > (block head (format t “Here we go.”) (return-from head ‘idea) (format t “We’ll never see this.”)) Here we go. IDEA Calling return-from allows your code to make a sudden but graceful exit from anywhere in a body of code Expressions after the return-from are not evaluated

Control-Blocks > (block nil (return 27)) 27 > (dolist (x ‘(a b c d e)) (format t “~A “ x) (if (eql x ‘c) (return ‘done))) A B C DONE

Control-Blocks The body of a function defined with defun is implicitly enclosed in a block with the same name as the function (defun foo ( ) (return-from foo 27) ) (defun read-integer (str) (let ((accum 0)) (dotimes (pos (length str)) (let ((i (digit-char-p (char str pos)))) (if i (setf accum (+ (* accum 10) i)) (return-from read-integer nil)))) accum))

Control-Blocks Within tagbody, you can use go > (tagbody (setf x 0) top (setf x (+ x 1)) (format t “~A “ x) (if (< x 10) (go top))) This operator is mainly something that other operators are built upon, not something you would use yourself Ugly code!!

Control-Blocks How to decide which block construct to use? Nearly all the time you’ll use progn If you want to allow for sudden exits, use block Most programmers will never use tagbody explicitly

Control-Context let Takes a body of code Allows us to establish variables for use within the body > (let ((x 7) (y 2)) (format t “Number”) (+ x y)) Number 9

Control-Context > ((lambda (x) (+ x 1)) 3) 4 ((lambda (x y) (format t “Number”) (+ x y)) 7 2)

Control-Context One let-created variable can’t depend on other variables created by the same let (let ((x 2) (y (+ x 1))) (+ x y)) ((lambda (x y) (+ x y)) 2 (+ x 1)) > (let* ((x 1) (y (+ x 1))) (+ x y)) 3 equivalent

Control-Context A let* is functionally equivalent to a series of nested lets (let ((x 1)) (let ((y (+ x 1))) (+ x y))) In both let and let*, initial values default to nil > (let (x y) (list x y)) (NIL NIL)

Control- Conditionals (if (oddp that) (progn (format t “Hmm, that’s odd.”) (+ that 1))) (when (oddp that) (format t “Hmm, that’s odd.”) (+ that 1)) equivalent

Control- Conditionals (if ) (if )  (when ) (if nil )  (unless )

Control- Conditionals (defun our-member (obj lst) (if (atom lst) nil (if (eql (car lst) obj) lst (our-member obj (cdr lst)))) (defun our-member (obj lst) (cond ((atom lst) nil) ((eql (car lst) obj) lst) (t (our-member obj (cdr lst))))) equivalent A Common Lisp implementation will probably implement cond by translating it to the “if” format

Control- Conditionals cond (cond ( …) ( …) … ( …) );cond The conditions are evaluated in order until one of them returns true When one condition returns true, the expressions associated with it are evaluated in order, and the value of the last is returns as the value of the cond > (cond (99)) ; if there are no expressions after the successful condition 99 ;, the value of the condition itself is returned.

Control- Conditionals case (case ( …) ( …)... ( …) ) ;case

Control- Conditionals (defun month-length (mon) (case mon ((jan mar may jul aug oct dec) 31) ((apr jun sept nov) 30) (feb (if (leap-year) 29 28)) (otherwise “unknown month”))) Keys Are treated as constants Will not be evaluated > (case 99 (99)) The successful clause contains only keys NIL

Control- Iteration do (do (( ) ( ) … ( )) ( ) ) ;do > (let ((x ‘a)) (do ((x 1 (+ x 1)) (y x x)) ((> x 5)) (format t “(~A ~A) “ x y))) (1 A) (2 1) (3 2) (4 3) (5 4) ;on each iteration, x gets its previous NIL ;value plus 1; y also gets the previous ;value

Control- Iteration do* Has the same relation to do as let* does to let > (do* ((x 1 (+ x 1)) (y x x)) ((> x 5)) (format t “(~A ~A) “ x y)) (1 1) (2 2) (3 3) (4 4) (5 5) NIL

Control- Iteration dolist > (dolist (x ‘(a b c d) ‘done) (format t “~A “ x)) A B C D DONE dotimes > (dotimes (x 5 x) ; for x = 0 to 5-1, return x (format t “~A “ x))

Control- Multiple values In Common Lisp, an expression can return zero or more values E.g., get-decoded-time returns the current time in nine values value Returns multiple values > (values ‘a nil (+ 2 4)) A NIL 6

Control- Multiple values > ((lambda ( ) (values 1 2) )) 1; 2 If something is expecting only one value, all but the first will be discarded > (let ((x (values 1 2))) x) 1

Control- Multiple values > (values) returns no value > (let ((x (values))) x) NIL Use multiple-value-bind to receive multiple values > (multiple-value-bind (x y z) (values 1 2 3) (list x y z)) (1 2 3) > (multiple-value-bind (x y z) (values 1 2) (list x y z)) (1 2 NIL)

Control- Multiple values > (multiple-value-bind (s m h) (get-decoded-time) (format nil “~A:~A:~A” h m s)) “4:32:13” We can pass on multiple values as the arguments to a second function using multiple-value-call > (multiple-value-call #’+ (values 1 2 3)) 6 multiple-value-list is like using multiple-value-call with #’list as the first argument > (multiple-value-list (values ‘a ‘b ‘c)) (A B C)

Control-Aborts catch and throw (defun super ( ) (catch ‘abort (sub) (format t “We’ll never see this.”))) (defun sub ( ) (throw ‘abort 99) > (super) 99

Control- Example: Date arithmetic (defconstant month #( )) (defconstant yzero 2000) (defun leap? (y) (and (zerop (mod y 4)) (or (zerop (mod y 400)) (not (zerop (mod y 100))))))

Control- Example: Date arithmetic (defun date->num (d m y) (+ (- d 1) (month-num m y) (year-num y))) (defun month-num (m y) (+ (svref month (- m 1)) (if (and (> m 2) (leap? y)) 1 0))) (defun year-num (y) (let ((d 0)) (if (>= y yzero) (dotimes (i (- y yzero) d) (incf d (year-days (+ yzero i)))) (dotimes (i (- yzero y) (- d)) (incf d (year-days (+ y i))))))) (defun year-days (y) (if (leap? y) ))

Control- Example: Date arithmetic (defun num->date (n) (multiple-value-bind (y left) (num-year n) (multiple-value-bind (m d) (num-month left y) (values d m y)))) (defun num-year (n) (if (< n 0) (do* ((y (- yzero 1) (- y 1)) (d (- (year-days y)) (- d (year-days y)))) ((<= d n) (values y (- n d)))) (do* ((y yzero (+ y 1)) (prev 0 d) (d (year-days y) (+ d (year-days y)))) ((> d n) (values y (- n prev))))))

Control- Example: Date arithmetic (defun num-month (n y) (if (leap? y) (cond ((= n 59) (values 2 29)) ((> n 59) (nmon (- n 1))) (t (nmon n))) (nmon n))) (defun nmon (n) (let ((m (position n month :test #’<))) (values m (+ 1 (- n (svref month (- m 1))))))) (defun date+ (d m y n) (num->date (+ (date->num d m y) n)))

Control- Example: Date arithmetic > (mapcar #’leap? ‘( )) (T NIL T) > (multiple-value-list (date )) ( )

Control Homework (Due April 7) Rewrite month-mon to use case instead of svref Define a single recursive function that returns, as two values, the maximum and minimum elements of a vector