ANSI Common Lisp 5. Control 16 June 2003. Blocks -- progn progn > (progn (format t “a”) (format t “b”) (+ 11 12)) ab 23 The expressions within its body.

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.
Semantic Analysis and Symbol Tables
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.
Pointers and Arrays Chapter 12
Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
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:
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
How to load a program file? Lisp programs in Allegro are saved under the file extension.cl. To load a file into the Lisp console, use the following: (load.
It is suggested that you use the Lisp interpreter available on the general machine (general.asu.edu). You are welcome to use other interpreters while developing,
Fall 2008Programming Development Techniques 1 Topic 18 Environment Model of Evaluation Section 3.2 Acknowledgement: This lecture (and also much of the.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
For Wednesday Read Chapter 4, sections 1 and 2 Homework: –Lisp handout 3.
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 (... ).....
Error Handling Common Lisp has the throw/catch statements so that you can simulate exception handling as seen in languages like Java But CL goes far beyond.
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,
Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin.
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.
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.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
PRACTICAL COMMON LISP Peter Seibel 1.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
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
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
CS314 – Section 5 Recitation 9
Jan 2016 Solar Lunar Data.
Example of formula (defun roots (a b c) (list
Getting Started with Lisp
Q1 Jan Feb Mar ENTER TEXT HERE Notes

Average Monthly Temperature and Rainfall
2017 Jan Sun Mon Tue Wed Thu Fri Sat
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
Free PPT Diagrams : ALLPPT.com
CSE S. Tanimoto Explicit Function Application
Jan Sun Mon Tue Wed Thu Fri Sat
Lisp: Using Functions as Data
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
Peter Seibel Practical Common Lisp Peter Seibel
Abstraction and Repetition
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
Free PPT Diagrams : ALLPPT.com

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
TIMELINE NAME OF PROJECT Today 2016 Jan Feb Mar Apr May Jun
The general format of case is the following: (case <key form>
Presentation transcript:

ANSI Common Lisp 5. Control 16 June 2003

Blocks -- progn progn > (progn (format t “a”) (format t “b”) ( )) ab 23 The expressions within its body are evaluated in order, and the last value is returned.

Blocks -- progn For readability purpose, don’t use progn if there is an alternative (let, cond, …). (if (FOO x) (progn (print “hello”) 23) 34) (cond ((FOO x) (print “hello”) 23) (t 34))

Blocks -- block A block has a name, which is the first argument of block. At any point of the block body, return-from can halt evaluation and return a value. (block head (format t “Here we go”) (return-from head ‘idea) (format t “This statement won’t be evaluated”)

Blocks -- block A block can be named nil, where the return macro, instead of return-from, can return a value. (block nil (return 27)) Implicit block in defun (defun foo () (return-from foo 27)) (block foo (return-from foo 27))

Blocks -- block Implicit block named nil enclosed in all iterations. (dolist (x ‘(a b c d e)) (format t “~A “ x) (if (eql x ‘c) (return ‘done))) A B C DONE Neither return-from nor return will work outside of an explicit or implicit block. WRONG CODE: (mapcar #'(lambda (x) (if (eql x 'c) (return 'done) (format t "~A " x))) '(a b c d e))

Context -- let The let creates a new lexical context (group expressions). Within its body, new variables can be defined. (let ((x 7) (y 2)) (+ x y)) The variables defined in let body are local to the body. The variables from outer context are visible the let body.

Context -- let The let expression is essentially a lambda function call. (let ((x 7) (y 2)) (+ x y)) ((lambda (x y) (+ x y)) 7 2)

Context -- let The value of one let-created variable does NOT depend on other variables created by the same let. (let ((x 1)) (let ((x 2) (y (+ x 1))) y)) 2

Context -- let If we want the value of one let-created variable does depend on other variables created by the same let, we can use let*. (let ((x 1)) (let* ((x 2) (y (+ x 1))) y)) 3

Context – destructuring-bind The destructuring-bind is used to bind tree values to corresponding parts of tree. >(destructuring-bind (w (x y). z) ‘(a (b c) d e) (A B C (D E))

Conditional – if, when, unless if (if (oddp that) (format t “odd”)) when (when (oddp that) (format t “Hmm, that’s odd.”) (+ that 1)) unless (unless (oddp that) (format t “Hmm, that’s even.”) (+ that 1))

Conditional – cond, case cond (cond ((predicate 1) expressions 1)) ((predicate 2) (expression2 2)) … (t (expressions N))) case – compare a value against a series of constants (won’t be evaluated). (case mon ((jan mar may jul aug oct dec) 31) ((apr jun sept nov) 30) (feb (if (leap-year) 29 28)))

Iteration – do, do* Basic usage (do ((i 1 (+ i 1))) ((> i 5) ‘done) (format t “~A ~A~%” i (* i i)))) More variable updates (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)

Iteration – do, do* do* (let ((x ‘a)) (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)

Iteration – dolist, dotimes dolist (dolist (x ‘(a b c d) ‘done) (format t “~A “ x)) A B C D DONE dotimes (iterates from 0 to n-1) (dotimes (x 5 x) (format t “~A “ x))

Iteration – mapc, mapcar mapc Always returns its second argument. (mapc #’(lambda (x y) (format t “~A “A “ x y)) ‘(hip flip slip) ‘(hop flop slop)) HIP HOP FLIP FLOP SLIP SLOP (hip flip slip) mapcar Takes a function and one or more lists as arguments, and returns the result of applying the function to elements taken from each list, until some list runs out. (mapcar #’list ‘( ) ‘(a b c)) ((1 A) (2 B) (3 C))

Multiple Values In CLISP, a function can return multiple values. The values function (values ‘a NIL (+ 2 4)) A NIL 6 If only one value is expected, all but the first will be discarded. (let ((x (values 1 2 3))) x) 1

Multiple Values The multiple-value-bind can receive multiple values (multiple-value-bind (x y z n) (values 1 2 3) (list x y z n)) (1 2 3 NIL) The multiple-value-call function (multiple-value-call #’+ (values 1 2 3)) 6 The multiple-value-list function (multiple-value-list (values ‘a ‘b ‘c)) (A B C)

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

Aborts – error, unwind-protect error The error function interrupts execution and pass the control to LISP break loop. unwind-protect The unwind-protect function ensures the remaining expressions will be evaluated even if the previous evaluation interrupts execution. The unwind-protect function is useful for clean- up or reset. For example, close the I/O stream when some errors come out.