Lecture #2 Kenneth W. Flynn RPI CS

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

Lecture #5 Kenneth W. Flynn RPI CS

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

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

Allegro CL Certification Program

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

Lisp. Versions of LISP

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

Lecture Notes on Lisp A Brief Introduction

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

Common Lisp. Blake McBride

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

Common LISP Tutorial 1 (Basic)

CSCI337 Organisation of Programming Languages LISP

A little bit of Lisp

Artificial Intelligence Programming

Imperative, OO and Functional Languages A C program is

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

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

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

Introduction to Functional Programming and basic Lisp

Allegro CL Certification Program

Lisp Basic Example Test Questions

Introduction to Lisp

Allegro CL Certification Program

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

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

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

INF4820. Common Lisp: Closures and Macros

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4

CS 314 Principles of Programming Languages

A Quick Introduction to Common Lisp

Symbolic Computation and Common Lisp

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

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

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

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

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

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Common LISP-Introduction

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

Robot Programming with Lisp

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

CMSC 331 Final Exam Fall 2013

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

Scheme: Strings Scheme: I/O

by the evening of Tuesday, Feb 6

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

John McCarthy IBM 704

Project 2: Scheme Interpreter

A Genetic Algorithm Implementation

Functional programming with Common Lisp

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

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

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

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

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

Functional Programming. Pure Functional Languages

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

CS 314 Principles of Programming Languages. Lecture 16

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

COMPUTER SCIENCE IN THE NEWS. CS61A Lecture 21 Scheme TODAY REVIEW: DISPATCH DICTIONARIES DISPATCH DICTIONARIES 7/27/2012

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?

Introduction to Scheme

MIDTERM EXAMINATION - CS130 - Spring 2005


CS 314 Principles of Programming Languages

FP Foundations, Scheme

Pattern Matching WIlensky Chapter 21

CS 842 Ben Cassell University of Waterloo

AllegroCache: an Introduction

User-defined Functions. Conditional Expressions in Scheme

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

Midterm Examination (Sample Solutions), Cmput 325

Lambda Calculus see notes on Lambda Calculus

Streams and Lazy Evaluation in Lisp

ALISP interpreter in Awk

Documentation for LISP in BASIC

SCHEME AND CALCULATOR 5b

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

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

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

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

CS A331 Programming Language Concepts

Introductory Scheme. Revision 1

Homework #2. Source code with description. Tzewen Wang ECE578 Winter 2005

CS 61A Discussion 8: Scheme. March 23, 2017

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

Heap storage. Dynamic allocation and stacks are generally incompatible.

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

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

CS61A Notes 02b Fake Plastic Trees. 2. (cons ((1 a) (2 o)) (3 g)) 3. (list ((1 a) (2 o)) (3 g)) 4. (append ((1 a) (2 o)) (3 g))

Scheme: Expressions & Procedures

CSc 520 Principles of Programming Languages

Lists in Lisp and Scheme

April 2 to April 4, 2018

Functional Programming. Pure Functional Languages

PAIRS AND LISTS 6. GEORGE WANG Department of Electrical Engineering and Computer Sciences University of California, Berkeley

Discussion 4. Data Abstraction and Sequences

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

Transcription:

Outline Programming in Lisp Lecture #2 Kenneth W. Flynn RPI CS Items from last time Recursion, briefly How to run Lisp I/O, Variables and other miscellany Lists Arrays Other data structures Jin Li lij3@rpi.edu Office Hours: M: 2-3:30 pm W 10 am - 12 pm Office:??? A Items From Last ime I (cdr nil Returns nil Unlimited arguments? &rest We'll talk about this next week Items From Last ime II -- (listp Items From Last ime III -- ' > (listp 'a > (listp 1 > (listp '(1 2 3 > (listp '( > (listp nil > (listp '(HELLO > (listp (5 ;; Error: he following characters cannot appear in symbols: ( ; " ' `, # So, '*hi* is valid: > (listp '*hi* > Kenneth W. Flynn (1-6

Recursion, Briefly Frequently we'll write recursive Lisp functions Recursive functions should have Basis case (at least one Recursive case Don't forget terminating condition How o Run Lisp Under UNIX kcl, gcl Specify in homework which used :q if you make an error ^D to exit Under Win '95 Goto http://www.franz.com/dload/dload.html Select Allegro CL Lite for Windows How o Write Lisp Use a text editor with paren matching! vi :set sm emacs Others? Load code into Lisp and then try it... load Allows you to not write programs at the top-level Reads a file and executes commands inside the file as if you typed them at the top-level > (load "hello.lsp" t Output With format Output is done with the format command (format destination format-string args... Destination is "t" for the console Format string is similar to C's printf function Usually returns nil, but we don't care! format Examples I >(format t "~%Hello World.~%" Hello World. > (format t "~%wo plus two is ~A.~%" (+ 2 2 wo plus two is 4. Kenneth W. Flynn (7-12

format Examples II > (format t "~%Words fail, buildings ~A." "tumble" Words fail, buildings tumble. > (format t "~%he ~A opens ~A~%" "ground" "wide." he ground opens wide. read and read-line read "Incredibly powerful" says the text. Reads input and parses into Lisp objects read-line Reads up to a newline; puts input into string Prefered for reading from console read and read-line Examples > (read2 2 > (readhello HELLO > (read-linehello World "Hello World" progn Used to create a "block" Allows side-effects Value of last expression evaluated is returned Avoid if possible; frequently needed for debuging output, etc. progn Example (defun stupid-hello ( (progn (format t "~%Hello" (format t " World~%" So Many Forms of Equality... For numbers, you have (= args... > (= 1 1 1 t For others you have: eq, eql, equal, equalp eq: Implementationly identical (rarely used eql: Logically identical (what we were thinking equal: Object identical (lists Stick with equal (more info on Steele 103-110 Kenneth W. Flynn (13-18

Equality Examples > (equal '(1 2 3 '(1 2 3 > (eq '(1 2 3 '(1 2 3 > (equal "Hello" "Hello" > (= 1 1.0 setf Assigns value to variable Side-effect > (setf x '(1 2 3 (1 2 3 > x (1 2 3 let Introduces new local variables Form (let Variable-Bindings-List Expressions* Variable-Bindings-List is a list of pairs of variables and expressions to set them equal to. hese are your new local variables Implicit progn let Example > (let ((x "Hello" (y " World" (format t "~%~A~A~%" x y Hello World Lists Lisp Lists I Lisp lists List construction functions Access (review Mapping functions Sets, Sequences Dotted lists cons cdr 1 2 3 car nil Kenneth W. Flynn (19-24

Lisp Lists II A "cons" refers to a pair of pointers he first pointer may point to data or another cons he second may point to data, another cons, or nil cons is used to construct such a pair car refers to the first pointer cdr refers to the second pointer List Construction Functions copy-list literally copies a list (copy-list list append copies the list arguments onto the beginning of the last list argument (append list1 list2 list3 list1 -> list2 -> list3 Don't forget list and cons Access (Review car and cdr (first and rest, first, second, third... nth returns nth car in the list >(nth 2 '(1 2 3 3 nthcdr returns the nth cdr in the list (confused? >(nthcdr 2 '(1 2 3 4 (3 4 last returns the last cons in the list Mapping Functions All about mapcar mapcar is used to apply a function to each element in one or more lists mapcar's first argument is a function One by one, the nth arguments of each list are passed to the function Function Passing: #' #' Sharp Quote All functions can be passed as parameters #'+ #'- #'list #'my-function Used in many standard functions Generics... lambda Functions (A rose without a name... Sometimes you create a function just to pass it to something like mapcar Instead of naming the function, you can create a function with no name -- a lambda function Simply use the special symbol lambda instead of the function name #'(lambda (x y (+ x y is our old friend the adder Kenneth W. Flynn (25-30

mapcar Examples > (mapcar #'+ '(1 2 '(1 2 (2 4 > (mapcar #'(lambda (x y (+ x y '(1 2 '(1 2 (2 4 member (member object list returns a cons begining with object if present member takes several keyword arguments Keyword arguments are of the form :keyword key-value :test equilvalance-function :key function-to-be-applied-first Order is irrelevant member Examples I > (member 2 '(1 2 3 (2 3 > (member 3 '(1 2 3 :key #'(lambda (x (+ x 1 (2 3 member Examples II > (member '(1 2 '((2 3 (1 2 > (member '(1 2 '((2 3 (1 2 :test #'equal ((1 2 Sequences length (length '(1 2 3 returns 3 reverse (reverse '(1 2 3 returns (3 2 1 (sort list sort-function (sort '(3 1 2 #'> returns (3 2 1 Dotted Lists Proper list refers to a list in which every cdr points to another cons (or nil Dotted list refers to the case when this is not true (1. 2 1 2 Kenneth W. Flynn (31-36

o Dot Or Not o Dot? Lisp displays a list with the dot only for the cons which is not proper So a cons can be used as a two field data structure. Better ways of doing this, though... Structures (next week come to mind Arrays Creation make-array 1 required argument -- list of dimensions or integer :initial-element initializes array Retrieval aref Returns reference to element Array Example > (setf x (make-array 3 :initial-element 0 #(0 0 0 > (setf (aref x 1 1 1 > (setf (aref x 2 2 2 > x #(0 1 2 Vectors Just one dimensional arrays Create with vector Simliar to list Can access quickly with svref instead of aref > (setf x (vector 1 2 3 #(1 2 3 > (svref x 2 3 Whew! We've covered a lot today! For next week Read Chapters 3 and 4 in Graham Start Homework #1 (Due 9/14. New due date! On the next exciting episode Structures Control Flow Gory function details Kenneth W. Flynn (37-42