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

Similar documents
Common LISP Tutorial 1 (Basic)

Common Lisp. Blake McBride

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Common LISP-Introduction

Lecture Notes on Lisp A Brief Introduction

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

CSCI337 Organisation of Programming Languages LISP

Allegro CL Certification Program

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

SCHEME AND CALCULATOR 5b

INF4820. Common Lisp: Closures and Macros

Imperative, OO and Functional Languages A C program is

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

Functions, Conditionals & Predicates

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

CS 360 Programming Languages Interpreters

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

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

Recursion & Iteration

Functional programming with Common Lisp

Lecture #2 Kenneth W. Flynn RPI CS

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

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

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

Lisp. Versions of LISP

Artificial Intelligence Programming

A little bit of Lisp

An Explicit-Continuation Metacircular Evaluator

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

Intro. Scheme Basics. scm> 5 5. scm>

Lisp Basic Example Test Questions

FP Foundations, Scheme

CSc 520 Principles of Programming Languages

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Functional Languages. Hwansoo Han

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

User-defined Functions. Conditional Expressions in Scheme

Review of Functional Programming

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Functional Programming. Pure Functional Languages

Putting the fun in functional programming

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

A Brief Introduction to Scheme (II)

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CS61A Midterm 2 Review (v1.1)

15 Unification and Embedded Languages in Lisp

Functional Programming

Symbolic Computation and Common Lisp

Announcements. Today s Menu

(defmacro while (condition &body body) `(iterate loop () (if,condition (loop)))))

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

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

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

A Quick Introduction to Common Lisp

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

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

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

A Genetic Algorithm Implementation

Scheme: Expressions & Procedures

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Functional Programming. Pure Functional Languages

CS 314 Principles of Programming Languages

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

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

An introduction to Scheme

Functional Programming. Big Picture. Design of Programming Languages

Allegro CL Certification Program

An Explicit Continuation Evaluator for Scheme

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

CSC 533: Programming Languages. Spring 2015

Lambda Calculus see notes on Lambda Calculus

Introduction to Lisp

Introductory Scheme. Revision 1

Allegro CL Certification Program

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Heap storage. Dynamic allocation and stacks are generally incompatible.

John McCarthy IBM 704

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

Chapter 1. Fundamentals of Higher Order Programming

Organization of Programming Languages CS3200/5200N. Lecture 11

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Functional Programming. Pure Functional Programming

Fifth Generation CS 4100 LISP. What do we need? Example LISP Program 11/13/13. Chapter 9: List Processing: LISP. Central Idea: Function Application

Vanilla Lisp Shell (VLS)

Robot Programming with Lisp

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Documentation for LISP in BASIC

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Functional Programming with Common Lisp

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Programming Languages


Transcription:

Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also a commercial version of Common Lisp which is free to students: Allegro Common Lisp Supports Linux, windows, FreeBSD, Mac OS X http://www.franz.com/downloads 1 2 Outline of Writing and Running Lisp 1. Write a program (function definitions in a file: blah.lsp (defun mysq (x (* x x (defun mytest (x (if (> x 10 Blah Poo 2. Run lisp /opt/apps/cmucl/bin/lisp 3. Load function definitions (load "blah.lsp" 4. Run functions (mysq 10 (mytest 2 3 LISP: A Quick Overview Components: Atoms, Lists, and Functions. Basics: list, math, etc. Arrays and SETQ vs. SETF Variable binding Lexical vs. dynamic scope Conditionals, predicates, iterations, etc. User-defined function Recursion Output 4

Components Symbolic expression = ATOM or LIST. Atom: numbers, variable names, etc. [<letters> <digits>]+ e.g.: 1, 10, foo, bar, this-is-an-atom List: functions, list of items "(" [<list> <atom>]* "" e.g.: (a, (1 (1 2 3 (4 6 NIL: it is an atom and at the same time a list. NIL is the same as ( Basics quote: returns a literal (i.e. not evaluated atom or a list. (+ 2 3 (+ 2 3 (quote (+ 2 3 (+ 2 3 Compare with: (+ 2 3 (eval (+ 2 3 Basically, you can think of a quoted atom or list as data, as opposed to instruction, in Lisp. T: true, as opposed to NIL. See conditionals and predicates. 6 Evaluation in Lisp Lisp basically tries to evaluate everything (atom or a list that is not quoted. If it sees an atom, it treats it as a variable, and tries to find out a value assigned to it. If it sees a list, it treats it as a function, where the first element in the list is seen as the function name and the rest function arguments. The quote function is used to exactly avoid such behavior (i.e., evaluate by default. Evaluation in Lisp (cont d For example, if you typed in (hello (my world, 1. Lisp will see the first entry in that list as a function and tries to evaluate it using the argument (my world. 2. But, it needs to evaluate all of the arguments first, so it will try to evaluate (my world. 3. Since this also looks like a function, Lisp will now try to evaluate function my. 4. To do that, it needs to evaluate the symbol world. Since it is an atom, Lisp will check if any value is assigned to the symbol world (i.e., treating it as a variable. What about ((hello world (my friend? 7 8

Evaluation in Lisp (cont d What about (* 10 b? Lisp sees a well-defined function * and proceeds to evaluate its arguments first. It is happy with the number 10, so it proceeds on to evaluate b. List (a (b (c d e (f g a (b (c d e (f g Here s where the problem begins. If you already did something like (setq b 20, then Lisp knows b can be evaluated to the value 20, so it will do that and evaluate * with that and return 200. If you haven t defined b, Lisp will treat it as an unbound variable, and balk. What about (* 10 a? 9 (c d b c d e List can be seen as trees: atoms at leaves and internal nodes representing lists. Once this is understood, the list operations such as car, cdr, cons become easy to understand. Exercise: draw the tree for (((((((a 10 f g Basics: List car: returns first element (atom or list (car (a (b c A (car ((b c a (B C cdr: returns all except the first element of a list, as a list (cdr (a (b c ((B C (cdr ((b c a (A Basics: List Combinations are possible: cxxxxr where X=(a d (cadr (a (b c == (car (cdr (a (b c (B C list: creates a list out of atoms and lists (list a (1 2 ((3 (7 8 (A (1 2 ((3 (7 8 length: number of elements in a list (length (a b c 3 Some shorthands: first, second, third,..., nth, rest (first (a b A (nth 2 (a b c d B 11 12

Basics: List Basics: Assignments/Arrays CONS: append an atom (or a list and a list (cons a (1 2 3 -> (A 1 2 3 (cons (a (1 2 3 -> ((A 1 2 3 APPEND: append two lists (append (1 2 (4 -> (1 2 4 setq: assignment of value to a symbol (setq x 10 10 x 10 setf: can set the value of a symbol (== setq or location or structure (next slide. Arrays and SETQ vs. SETF make-array : create an array aref : array reference setf : set value of array element 13 14 Note: * is the Lisp prompt. Arrays and SETQ vs. SETF * (setq a (make-array (3 3 #2A((NIL NIL NIL (NIL NIL NIL (NIL NIL NIL * (aref a 2 2 NIL * (setf (aref a 2 2 1000 1000 * a #2A((NIL NIL NIL (NIL NIL NIL (NIL NIL 1000 * (setq (aref a 2 2 1000 Error: (AREF A 2... is not a symbol.... More Fun with SETF Replace list element with SETF. Note: SETQ will not work! *(setf b (1 (2 3 4 (1 (2 3 4 *(caadr b 2 *(setf (caadr b abcdefg ABCDEFG *b (1 (ABCDEFG 3 4 1 16

Basics: Math Function (+ 1 2 (* 3 4 (+ (* 2 3 (/ 4 etc. (max 1 2 3 4 (min 4 6 (sqrt 16 (expt 2 3 (round 3.14192 Basics: File Loading (load "filename" defun : user defined function * (defun mult (x y (* x y DEFUN * (mult 10 20 200 Use the let and let* forms: (defun mult (x y (let ((tx x (ty y (* tx ty 17 18 Fibonacci number: Recursion F(N = F(N-1 + F(N-2, F(1=1, F(2=2. (defun fibo (x (cond ((equal x 1 1 ((equal x 2 2 ((> x 2 (+ (fibo (- x 1 (fibo (- x 2 *(fibo 4 *(fibo 8 19 Binding You can bind variables anywhere in a program with the let or let* special forms to create a local context. let and let* : lexical scope (local context (let (local var list BODY (let ((x 10 y (z 20 BODY (let* ((x 10 (y (* 2 x z BODY Either just a variable or (variable default-value. With let*, values from previous vars can be used to define new value. (let* ((x 10 (y (* 2 x z BODY 20

Use of Local Scope Always use (let... or (let*... be your first (and only statement in your function, if you are writing something complex which is not like a mathematical function in its usual sense. Think of it as declaring local variables in C/C++. (defun func-name (arg1 arg2 (let (localx localy localz (expression1 args (expression2 args (expression3 args (expression4 args (expression args 21 Binding: Example * (let ((a 3 (+ a 1 4 * (let ((a 2 (b 3 (c 0 (setq c (+ a b c * (setq c 4 4 * (let ((c c * c 4 22 Lexical Scope Return value according to the lexical scope where it was defined. * (setq regular * (defun check-regular ( regular CHECK-REGULAR * (check-regular * (let ((regular 6 (check-regular 23 Dynamic Scope Use the defvar to define a special variable that is dynamically scoped. (Just think of it as defining a global variable. * (defvar *special* *SPECIAL* * (defun check-special ( *special* CHECK-SPECIAL * (check-special * (let ((*special* 6 (check-special 6 * *special* *(let ((x 23 (check-special 24

Group (or Block of Commands How Not to Define a Block progn returns the result of the last element, but evaluates all s-expressions in the argument list. (progn (setq a 123 (* 10 0 a 123 A better way of writing it is: (progn (setq a 123 (* 10 A common mistake is to define a block using just bare parentheses, instead of using the function (progn...: ( (setq x 10 (setq y 20 (* x y It looks fine, but as mentioned earlier, Lisp will interpret this list as a function that has a name (setq x 10 and two argument (setq y 20 and (* x y. So, don t do this! 2 26 Conditionals: the Ps. Control Flow p is for predicate: numberp, listp, symbolp, zerop,... common comparisons: <, >, equal : if the values are the same. eq : if the memory locations are the same. and, or, not : logical operators. Returns either NIL or T. IF STATEMENT (if (> 2 3 ; condition (+ 4 ; when true (* 4 ; when false SWITCH STATEMENT (cond ((testp1 (return-value1 ; condition 1 ((testp2 (return-value2 ; condition 2 ((testp3 (return-value3 ; condition 3 (t (default-value ; default 27 28

Iterations DOTIMES (dotimes (index-var upper-bound result-var BODY * (dotimes (k 1 val (setq val k 0 * (dotimes (k 10 val (setq val k 9 Also find out more about dolist, do, and loop. print : print a string (print "hello" Output format : format a string; (format dest string args dest: determines what to return t: return NIL, NIL: return string. % : insert CR S : S-expression A : ascii D : integer widthd : blank space e.g. D F : floating point width,decimalf : width and decimal point 29 30 Format: examples * (format t "Hello, world!" Hello, world! NIL * (format nil "Hello, world!" "Hello, world!" Format: examples * (format nil "The list is s and %the text is a" (list a b c "This is a string" "The list is (A B C and the text is This is a string" 31 32

Format: examples Dealing with Errors * a <--- errorneous input * (format nil "One: d %Two: f %Three:,2f" 12 (/ 4 3 (/ 4 3 "One: 12 Two:1.3333334 Three: 1.33" Error in KERNEL::UNBOUND-SYMBOL-ERROR-HANDLER: Restarts: 0: [ABORT] Return to Top-Level. Debug (type H for help (EVAL A Source: Error finding source: Error in function DEBUG::GET-FILE-TOP-LEVEL-FORM: target:code/eval.lisp. 0] q <--- go back to top level the variab Source * 33 34 Overview Some more LISP stuff: user input, trace, more setf, etc. Symbolic Differentiation: [q] does it need intelligence? Expression Simplification READ: keyboard input from user *(read hello HELLO READ: User Input *(if (equal (read hello good bad hello GOOD 3 36

*(trace fibo (FIBO *(fibo 4 1> (FIBO 4 2> (FIBO 3 3> (FIBO 2 <3 (FIBO 2 3> (FIBO 1 <3 (FIBO 1 <2 (FIBO 3 2> (FIBO 2 <2 (FIBO 2 <1 (FIBO TRACE/UNTRACE: call tracing * 37 Symbolic Differentiation Original concept and code borrowed from Gordon Novak s AI course at UTCS. 38 Symbolic Differentiation Basics: given variable x, functions f(x, g(x, and constant (i.e. number a: 1. 2. 3. da = 0, d(a x d(f + g d(f g = a = df + dg = df g + f dg The operators can be extended to: binary minus (e.g. (- x 1, unary minus (e.g. (- x, division (e.g. (/ x 10, etc. Describing in LISP (I (deriv <expression> <variable> 1. da = 0, d(a x (deriv 10 x -> 0 (deriv (* 10 x x -> 10 = a 39 40

Describing in LISP (II (deriv <expression> <variable> Describing in LISP (III (deriv <expression> <variable> 1. d(f + g = df + dg (deriv (+ (* x 10 (+ 2 x x == (list + (deriv (* x 10 x (deriv (+ 2 x x 1. d(f g = df g + f dg (deriv (* (+ 14 x (* x 17 x ==(list + (list * (deriv (* 14 x x (* x 17 (list * (+ 14 x (deriv (* x 17 x 41 42 DERIV: the core function Main Function: DERIV Pseudo code (basically a recursion: (defun deriv (expression var BODY 1. if expression is the same as var return 1 2. if expression is a number return 0 3. if (first expression is +, return (+ (deriv (second expression var (deriv (third expression var 4. and so on. 43 You can make separate functions for each operator: (defun deriv (expr var (if (atom expr (if (equal expr var 1 0 (cond ((eq + (first expr ; PLUS (derivplus expr var ((eq * (first expr ; MULT (derivmult expr var (t ; Invalid (error "Invalid Expression!" 44

Calling DERIV from DERIVPLUS Then, you can call deriv from derivplus, etc. (defun derivplus (expr var (list + (deriv (second expr var (deriv (third expr var Expression Simplification Problem: a lot of nested expression containing (* 1 x (* x 1 (+ 0 x (+ x 0 (+ 3 4... which are just x, x, x, x, and 7. Use simplification rules: 1. (+ <number> <number>: return the evaluated value 2. (* <number> <number>: return the evaluated value 3. (+ 0 <expr> (+ <expr> 0: return <expr> 4. (* 1 <expr> (* <expr> 1: return <expr>. (- (- <expr> : return <expr> HINT: look at the raw result and see what can be reduced. 4 46 SPLUS: Simplify (+ x y (defun splus (x y (if (numberp x (if (numberp y (+ x y (if (zerop x y (list + x y (if (and (numberp y (zerop y x (list + x y 47 Programming Exercise Symbolic differentiation: details TBA. 48