Announcements. Today s Menu

Similar documents
Lisp Basic Example Test Questions

Modern Programming Languages. Lecture LISP Programming Language An Introduction

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

FP Foundations, Scheme

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

Common Lisp. Blake McBride

Lecture Notes on Lisp A Brief Introduction

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

Common LISP-Introduction

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

INF4820. Common Lisp: Closures and Macros

Common LISP Tutorial 1 (Basic)

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

Lisp. Versions of LISP

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

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

ALISP interpreter in Awk

Symbols are more than variables. Symbols and Property Lists. Symbols are more than variables. Symbols are more than variables.

Functional programming with Common Lisp

Functional Programming. Pure Functional Programming

Associative Database Managment WIlensky Chapter 22

CSCI337 Organisation of Programming Languages LISP

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

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

Turtles All The Way Down

Imperative, OO and Functional Languages A C program is

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

Review of Functional Programming

Lambda Calculus see notes on Lambda Calculus

A little bit of Lisp

CS61A Midterm 2 Review (v1.1)

Robot Programming with Lisp

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

Associative Database Managment

Midterm Examination (Sample Solutions), Cmput 325

UMBC CMSC 331 Final Exam

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

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

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

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

Functional Programming. Pure Functional Languages

Project 2: Scheme Interpreter

Imperative languages

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

(penguin-rule (if (animal-x is bird) (animal-x can swim)) (then (animal-x is penguin))) (?x is bird)

Introduction to Lisp

Recursion & Iteration

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

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

Documentation for LISP in BASIC

Introduction to Functional Programming and basic Lisp

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

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

Func%onal Programming in Scheme and Lisp

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

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

An Explicit-Continuation Metacircular Evaluator

User-defined Functions. Conditional Expressions in Scheme

Func%onal Programming in Scheme and Lisp

Vectors in Scheme. Data Structures in Scheme

Announcements. Today s Menu

Functional Programming

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

MIDTERM EXAMINATION - CS130 - Spring 2005

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

Functional Programming. Big Picture. Design of Programming Languages

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

by the evening of Tuesday, Feb 6

Chapter 15 Functional Programming Languages

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSc 520 Principles of Programming Languages

Allegro CL Certification Program

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

Functional Programming. Pure Functional Languages

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

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

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

Functions, Conditionals & Predicates

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

CS 360 Programming Languages Interpreters

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

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

CS 314 Principles of Programming Languages. Lecture 16

19 Machine Learning in Lisp

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

Functional Programming with Common Lisp

UMBC CMSC 331 Final Exam

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

A Quick Introduction to Common Lisp

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

STREAMS 10. Basics of Streams. Practice with Streams COMPUTER SCIENCE 61AS. 1. What is a stream? 2. How does memoization work?

Evaluating Scheme Expressions

SOFTWARE ARCHITECTURE 6. LISP

Functional Programming. Contents

Basics of Using Lisp! Gunnar Gotshalks! BLU-1

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

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I

CS 314 Principles of Programming Languages

Introduction to Scheme

Functional Languages. Hwansoo Han

Transcription:

Announcements 1 Today s Menu Finish Introduction to ANNs (original material by Dr. Mike Nechyba) > Slides 58-60 > Adjusting the Learning Rate Loose Ends in > Lambda Expressions Review > I/O Functions > PROG > LOOPing 2

Finish Anns See Class 11 Notes Slides 34-36 ANN Example 3 LAMBDA Expressions allow us to define anonymous procedures The lambda expression is the actual mechanism that the interpreter uses to "evaluate" dummy arguments in a procedure. This is what X returns when you use FUNCTION- LAMBDA-EXPRESSION. Suppose we want to put quotes around all the elements of an input list. (defun put-quotes(lis) (mapcar #'qhelp lis)) (defun qhelp(sex) (list (quote quote) sex)) > (put-quotes '(this is a test)) ((QUOTE THIS) (QUOTE IS) (QUOTE A) (QUOTE TEST)) But if you begin to run out of names for your "helper" functions (especially if they are only used once in a program) a more elegant solution is given by: (defun put-quotes(lis) (mapcar #'(lambda(sex) (list (quote quote) sex)) lis )) 4

Lambda Expressions The actual mechanism that runs user-defined functions is the built-in lambda expression. For example, let us define a function to increment, say, inc. >(defun inc(x) (+ 1 x)) INC >(inc 1) 2 >(function-lambda-expression #'inc) (lambda(x) (+ 1 x)) What is stored in the system when you use function inc is the symbolic expression (lambda(x) (+ 1 x)). That is, inc and (lambda(x) (+ 1 x)) are one in the same. To prove it, replace inc with (lambda(x) (+ 1 x)) in the expression (inc 1). > ((lambda(x) (+ 1 x)) 1) 2 5 Lambda Expressions Lambda expressions can be used as anonymous functions anywhere you would otherwise use a named function. For example, using inc we can increment a list of integers using the built-in function mapcar. The syntax for mapcar is (mapcar function-f list-of-arguments-for-f ). The result is (list (f (first of list)) (f (second of list)) (f (third of list)) etc.) > (mapcar # oddp '(1 2 3)) (t nil t) > (mapcar #'inc '(1 2 3)) (2 3 4) > (mapcar #'(lambda(x) (+ 1 x)) '(1 2 3)) (2 3 4) This example shows that we did not need to do a defun to define inc and could just use the code for inc (its corresponding lambda expression) in the same manner as we used inc. This is more efficient because we save the space and OH of having to define the function inc in order to use it inside mapcar. 6

Lambda Expressions In some cases, to solve scoping problems we must use the lambda (anonymous function) instead of defun. For example, to do the intersection of two sets we need a helper function that checks to see if each element of s1 is in s2. If yes, it belongs in the intersection, if not, we do not keep it. Let me call the helper function finter. finter returns the element from s1 that is also inside s2 as a list or returns nil. > (defun inter(s1 s2) (mapcan # finter s1)) > (defun finter (sex) (cond ( (member sex s2) (list sex)) ( t nil) )) For example if s1 is (a b) and s2 is (a c), (finter a) would return (a) and (finter b) would return (). If I append these two answers, presto, I get the result of (inter (a b) (a c)) = (a) 7 Lambda Expressions But there is a problem. What is the s2 inside finter? You could say that s2 is global to finter. Using that line of reasoning we could write (inter s1 s2) as follows: > (defun inter(s1 s2) (mapcan # finter s1)) > (inter (a b) (a c)) ***ERROR*** What is the problem? When you call finter inside inter, s2 is known to inter but not to finter. The scope of s2 is inside the definition of inter and it is not made global to. But as we said earlier the s2 in finter is global to finter. The interpreter cannot resolve this problem. But if you use the anonymous version of finter inside inter, all is well. > (defun inter(s1 s2) (mapcan #'(lambda (sex) (cond ( (member sex s2) (list sex)) ( t nil) )) s1)) > (inter (a b) (a c)) (A) 8

General MAPing Functions MAPCAR and related mapping functions allow us to easily transform lists. MAPCAR takes two arguments, the first is a funarg and the second a list. The answer is a list of the results of applying the functional argument to each successive car of the 2 nd argument. Thus, (mapcar #'f '(x 1 x 2 x 3 )) ==> (list f(x 1 ) f(x 2 ) f(x 3 )) template: (mapcar #'<fname> <a list of SEXes for fname>) > (mapcar # oddp '(1 2 3)) ==> (T NIL T) We can define mapcar as follows: (defun mymapcar (f lis) (cond ( (null lis) nil ) ( t (cons (funcall f (car lis)) (mymapcar f (cdr lis))) ) )) > (mymapcar #'oddp '(1 2 3)) ==> (T NIL T) 9 User-Defined MAPing Functions Using our definition of MAPCAR we can define MAPPENDCAR, (MAPCAN) which applies f (.) to successive cdrs as follows: (defun mappendcar (f lis) (cond ( (null lis) nil ) ( t (append (funcall f (car lis)) (mappendcar f (cdr lis))) ) )) (defun inter (s1 s2) (mappendcar #'ihelp s1)) (defun ihelp (s1el) (cond ( (member s1el s2) (list s1el)) ( t nil) )) But this will not work either. Why? (Same as in Slide 7} 10

> (inter '(a b) '(b c)) Error: The variable S2 is unbound. Happened in: #<Closure-IHELP: #76dc8c The reason is obvious(?), s2 is only defined inside INTER but is undefined inside ihelp. We MUST use lambda: > (defun inter (s1 s2) (mappendcar #'(lambda (s1el) (cond ( (member s1el s2) (list s1el)) ( t nil))) s1)) INTER > (inter '(a b) '(b c)) (B) 11 MAPing Functions Example Use a mapping function to transpose an N N matrix represented as N lists of N integers each, e.g., Lisp (setf mat '((1 2 3) (4 5 6) (7 8 9))) ( (1 2 3) (4 5 6) (7 8 9) ) Lisp (transpose mat ) ( (1 4 7) (2 5 8) (3 6 9) ) Lisp (transpose '( (1 2) (3 4) (5 6) ) ) ( (1 3 5) (2 4 6) ) > (mapcar #'car mat) (1 4 7) > (mapcar #'cdr mat) ( (2 3) (5 6) (8 9) ) Lisp (defun transp (mat) (if (null (car mat)) nil (cons (mapcar #'car mat) (transp (mapcar #'cdr mat))))) Lisp (transp mat) ( (1 4 7) (2 5 8) (3 6 9) ) 12

Substop replaces a sex in a list at the top-level. (defun substop (this that lis) (cond ((null lis) nil) ((equal that (car lis)) (cons this (substop this that (cdr lis)))) ('else (cons (car lis) (substop this that (cdr lis)))) )) Let s try to write substop using a mapping function. (defun replacesex (sex3) (if (equal sex1 sex3) sex2 sex3)) /* sex1,2 are global! */ (defun substop (sex1 sex2 lis) (mapcar #'replacesex lis)) /* The following code will not run. Why? */ > (substop 'coke 'pepsi '(coke is it)) Error: The variable SEX1 is unbound. Happened in: #<Closure-REPLACESEX: #794b20> A Lambda Expression let us fix this nicely > (defun substop (sex1 sex2 lis) (mapcar #'(lambda (sex3) (if (equal sex1 sex3) sex2 sex3)) lis)) > (substop 'coke 'pepsi '(coke is it)) (PEPSI IS IT) 13 I/O I/O Functions in The following represents a sampling of the available I/O functions in. For a complete list see the Reference Manual. If the file pointers outf or inf are omitted, the system defaults to the terminal. (setf outf (open "fname" :direction :output)) Opens disk file fname for output. Sets outf, i.e., a file pointer for further use. (print Sex outf) Returns the value of the Sex and sends a copy to outf. (dribble "fname") Echoes all CRT I/O to the disk file (prin1 Sex outf) Same as print except for the trailing cr/lf (terpri outf) Issues a cr/lf (read inf) Inputs the next Sex from inf (or CRT if inf omitted) (read-line inf) Read 1 line from inf (read-char inf) Read 1 character from inf (close outf) Close file outf (or inf) 14

PROG The PROG construct allows for an indefinite number of forms to be evaluated with local variables. In modern it has been replaced by LET and LOOP constructs, but it is still useful. (prog (var1 var2... varn) <forms>) 1. The number of forms in prog is indeterminate. 2. PROG initializes all its local variables to nil (var1 var2... varn). 3. <Forms> are evaluated in order except: a. Atomic forms are not evaluated. They are treated as labels. b. (GO label) means continue from the form that follows label. c. (return expr) exits a prog evaluation & returns (eval expr). 4. If a prog runs out of forms, it returns nil. 5. If a cond runs out of stuff inside a prog it is treated as a NOOP. 15 Example: (defun fact (n) (prog (i j) (if (< n 0) (return nil) ) (setf i 1 j 1) ( if (< n 2) (return j) ) LOOP (setf i (+ i 1) j (* j i) ) (if (= i n) (return j)) (go LOOP) )) 16

LOOPING Common includes most(all) the most popular looping constructs template: (dotimes (<count atm> <upperbound form> <result form>) <body>) > (defun power (m n) (let ((result 1)) ( dotimes (count n result) (setf result (* m result)) ) )) Also template: (dolist (<element> <list form> <result form>) <body>) > (dolist (n '(1 2 3) 'end) (prin1 (list 'n= n 'n+1= (+ 1 n))) (prin1 '!)) (N= 1 N+1= 2)!(N= 2 N+1= 3)!(N= 3 N+1= 4)!END > (let ((n 0))(dolist (grade '((john a) (mary b) (james c) (dick a)) (list n 'non-agrades)) (if (equal (cadr grade) 'a) (print (list 'a (car grade))) (setf n (+ 1 n))) ) ) * Prints all the A grades in the input list & counts the non-a grades * EVALUATIONS (eval sex) hands sex to the interpreter if (setf x '(+ 1 2) ) then (eval x) returns 3 17 PROPERTY LISTS {Review} The ability to associate with an atom in a lisp form (pvalue, the property value) under a given atomic designation (pname, the property name). template: (putprop symb pvalue pname) {Requires 3 arguments} symb - must be atomic, i.e., the atom to receive the property. pvalue - the property value, which can be any sex. pname - must also be atomic, i.e., the property name. > (putprop 'cain 'adam 'father) ADAM > (putprop 'eve '(cain abel) 'mother-of ) (CAIN ABEL) > (setf eve 'first-woman) FIRST-WOMAN > eve FIRST-WOMAN 18

Putprop is not always included in some dialects. If not use: > (setf (get 'cain 'father) 'adam) > (setf (get 'eve 'mother-of) '(cain abel)) > (setf (get 'eve 'grandmother) '(tubalcain jubal)) (defun putprop (atm pvalue pname) (setf (get atm pname) pvalue)) In memory, property lists are stored as association lists or dotted pairs associated with the atom, e.g., ( eve (mother (cain abel)) (grandmother (tubalcain jubal)) ) 19 To retrieve properties use the function (get atm pname). It returns the property value if one has been defined or nil. Therefore, the system cannot distinguish between undefined properties and those whose value has been set to nil. A good rule to follow is to always set pvalue to something non-nil > (get 'cain 'father) ADAM > (get 'eve 'mother-of) (CAIN ABEL) > (get 'cain 'grandfather) NIL To remove a property use (remprop symb pname). Returns nil. > (remprop 'cain 'father) NIL > (get 'cain 'father) NIL 20

Another way to view this is as an associated list of pairs (a-list for short). Associated pairs are lists of sublists where the first element of each sublist is a "key or indicator" and the cdr of the sublist is the value. For example: > (setq eve '( (mother (cain abel)) (grandmother (tubalcain jubal)) )) ((MOTHER (CAIN ABEL)) (GRANDMOTHER (TUBALCAIN JUBAL))) > eve ((MOTHER (CAIN ABEL)) (GRANDMOTHER (TUBALCAIN JUBAL))) The function (assoc key a-list) returns the corresponding pair that has a key value matching argument. > (assoc 'mother eve) (MOTHER (CAIN ABEL)) (defun myassoc (key alist) (cond ( (null alist) nil ) ( (equal key (caar alist)) (car alist) ) ( t (myassoc key (cdr alist)) ) )) 21 The End! 22