66 2210 - Programming in Lisp; Instructor: Alok Mehta1 66-2210-01 Programming in Lisp Introduction to Lisp.

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

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.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
Names and Bindings.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
Common Lisp! John Paxton Montana State University Summer 2003.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
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)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
TES3111 Oct 2001 Data Structures S-Expression - Symbolic expression. It can be an Atom, a List or a collection of S- Expression enclosed by (…) Atom -
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.
TES3111 October 2001 Artificial Intelligence LISP.
CSCI Programming in Lisp; Instructor: Alok Mehta1 CSCI 2210: Programming in Lisp Introduction to Lisp.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
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.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
CS 462: Introduction to Artificial Intelligence This course advocates the physical-symbol system hypothesis formulated by Newell and Simon in It.
Digital Electronics Data Structures LISP
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
Copyright © Curt Hill Other Trees Applications of the Tree Structure.
Lists in Python.
Mastering STACKS AN INTRODUCTION TO STACKS Data Structures.
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
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.
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
CIS-165 C++ Programming I CIS-165 C++ Programming I Bergen Community College Prof. Faisal Aljamal.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Computer Programming for Biologists Class 3 Nov 13 th, 2014 Karsten Hokamp
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
ISBN Chapter 15 Functional Programming Languages.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Copyright © Curt Hill Other Trees Applications of the Tree Structure.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Section 15.4, 15.6 plus other materials
A I (Artificial Intelligence)
Lists in Lisp and Scheme
LISP LISt Processing.
Stacks Chapter 4.
LISP A brief overview.
First Lecture on Introductory Lisp
Lisp and Scheme I.
LISP A brief overview.
LISP LISt Processing.
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Introduction to C Programming
LISP primitives on sequences
Lists in Lisp and Scheme
Presentation transcript:

Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp

Programming in Lisp; Instructor: Alok Mehta2 What is Lisp?  Stands for LISt Processing  Used for symbol manipulation  Interactive (interpreted)  Easy to learn  Syntax and constructs are extremely simple  Helps make computers “Intelligent”

Programming in Lisp; Instructor: Alok Mehta3 Artificial Intelligence  Sample applications  Expert Problem Solvers (e.g. Calculus, Geometry, etc.)  Reasoning, Knowledge Representation  Learning  Education  Intelligent support systems  Natural Language interfaces  Speech  Vision

Programming in Lisp; Instructor: Alok Mehta4 Symbolic Expressions  Data and programs are represented uniformly  Expression that describes this course (course (name (Programming in Lisp)) (instructor (name (Alok Mehta)) ( (department (Computer Science)))  Expression to add (+ 3 2); Reverse polish notation!  Symbolic expressions: Atoms and Lists –Atoms - ‘course’, ‘Programming’, ‘+’, ‘7’ –Lists - ‘(+ 3 2)’, ‘(Programming in Lisp)’

Programming in Lisp; Instructor: Alok Mehta5 Calling Lisp Procedures  Lisp procedure calls are symbolic expressions  Represented using lists (like everything else)  Format of a Lisp procedure call – ( …)  Arithmetic expressions are in “Reverse Polish Notation” (+ 3 2); Returns 5 –Calls the Lisp procedure “+” with arguments “3” and “2” –The return value of the expression is 5 –The “+” procedure can take any number of arguments ( ); Returns 10

Programming in Lisp; Instructor: Alok Mehta6 Overview of Lisp Syntax  Overview of Lisp Syntax ( Left Parenthesis. Begins a list of items. Lists may be nested. ) Right Parenthesis. Ends a list of items. (* (+ 3 2) (+ 7 8)) ; Semicolon. Begins a comment (terminates at end of line) (* (+ 3 2) (+ 7 8)) ; Evaluate ((3+2)*(7+8)) " Double Quote. Surrounds character strings. "This is a thirty-nine character string." ’ Single (Forward) Quote. Don’t evaluate next expression '(Programming in Lisp)  Examples ”(+ 3 2)”; returns the string "(+ 3 2)” as an atom (+ 3 2); evaluates (+ 3 2) and returns 5 '(+ 3 2); returns the expression (+ 3 2) as a list  Lisp is case-insensitive

Programming in Lisp; Instructor: Alok Mehta7 Using Lisp on RCS  Conventions $UNIX Prompt >LISP Interpreter prompt  From a UNIX prompt, start the lisp interpreter $ gcl GCL (GNU Common Lisp) Version(2.2) Mon Sep 30 09:45:44 EDT 1996 Licensed under GNU Public Library License Contains Enhancements by W. Schelter >  At the Lisp prompt, type your Lisp Expressions > (* (+ 3 2) (+ 7 8)) 75 >  Lisp expressions return values  Return values can be used in other expressions

Programming in Lisp; Instructor: Alok Mehta8 Using Lisp on RCS  Recovering from errors in GCL (:q) > (+ 4 ’x) Error: "x" is not of type NUMBER. Fast links are on: do (si::use-fast-links nil) for debugging Error signalled by +. Broken at +. Type :H for Help. >> :q  Executing lisp commands from a file > (load "prog1.lsp") ** Reads and executes the lisp expressions contained in “prog1.lsp” **  Accessing on-line help > (help)  Exiting from GCL: “(bye)” or “CTRL-d” > (bye)

Programming in Lisp; Instructor: Alok Mehta9 Setf Assigns Variables  Setf (SET Field) assigns variables (side effect) > (setf a '(+ 5 3)); Lisp’s way of saying “a=5+3;” (+ 5 3) > (setf b (+ 5 3)) 8  Examining variables > a (+ 5 3) > b 8  Accessing variables > (+ 3 b) 11 > (+ 3 'b) ** error ** > (+ 3 a) ** error **

Programming in Lisp; Instructor: Alok Mehta10 Cons, Remove, First, Rest  Lists are used to represent knowledge > (setf complang '(C++ Lisp Java Cobol)) (C++ LISP JAVA COBOL)  Cons (CONStruct) adds an element to a list > (setf complang (cons 'Perl complang)) (PERL C++ LISP JAVA COBOL)  Remove removes an element from a list > (setf complang (remove 'Cobol complang)) (PERL C++ LISP JAVA)  First gets the first element of a list > (first complang) PERL  Rest gets everything except the first element > (rest complang) (C++ LISP JAVA)

Programming in Lisp; Instructor: Alok Mehta11 Lists are like boxes; NIL=Empty G H C D A B F IJ  Lists are like boxes; they can be nested ((( A B ) C D ( E ) ( )) ( F ) G H (((I)(J))))  ‘NIL’ is an empty list > (setf messy '(((A B) C D (E) ( )) (F) G H (((I)(J)))) ) (((A B) C D (E) NIL) (F) G H (((I)(J)))) > (first messy) ((A B) C D (E) NIL) E

Programming in Lisp; Instructor: Alok Mehta12 First, Rest Revisited  First returns the first element of a list  Returns an atom if the first element is an atom  Returns a list if the first element is a list  Rest returns all elements of a list except the first  Always returns a list  Examples > (first '((a) b)); returns (A) > (first '(a b)); returns A > (first '(a)) ; returns A > (first '( )); returns NIL > (rest '((a) b)); returns (B) > (rest '(a b)); returns (B) > (rest '(a)); returns NIL > (rest '( )); returns NIL

Programming in Lisp; Instructor: Alok Mehta13 Getting the second element  Use combinations of first and rest > (setf abcd '(a b c d)) (A B C D) > (first (rest abcd)) B > (first '(rest abcd)) REST; Quote stops expression from being evaluated!  Or, use second > (second abcd) B  third, fourth, …, tenth are also defined

Programming in Lisp; Instructor: Alok Mehta14 Exercises  Evaluate > (first '((a b) (c d))) > (first (rest (first '((a b) (c d)))))  Use First and Rest to get the symbol PEAR (apple orange pear grape) ((apple orange) (pear grapefruit)) (apple (orange) ((pear)) (((grapefruit))))  Other useful exercises  Text, 2-2, 2-3, 2-4

Programming in Lisp; Instructor: Alok Mehta15 Setf Revisited  Setf  Format (setf …)  Example > (setf x 0 y 0 z 2) 2  Returns –the value of the last element  Side effects –assigns values for symbols (or variables),, … –the symbol then becomes an atom that evaluates the value assigned to it > x 0

Programming in Lisp; Instructor: Alok Mehta16 List storage  Draw List Storage Diagram for (setf alist '(A (B (C))))  Explain semantics of functions  first, rest, cons, remove  Draw List Storage diagram for ((apple orange) (pear grapefruit)) A B C B C A alist Contents of Address Register (CAR) = Old name for “First” Contents of Decrement portion of Register (CDR) = Old name for “Rest” Cons Cell

Programming in Lisp; Instructor: Alok Mehta17 Append, List  Append  Combines the elements of lists > (append ’(a b c) ’(d e f)) (A B C D E F)  List  Creates a new list from its arguments > (list ’a ’b ’(c)) (A B (C)) ABCDEF

Programming in Lisp; Instructor: Alok Mehta18 Cons, Setf; Push; Pop  Cons has no side effects > (setf complang '(C++ Lisp Java Cobol)) (C++ LISP JAVA COBOL) > (cons ’Perl complang) (PERL C++ LISP JAVA COBOL) > complang (C++ LISP JAVA COBOL) > (setf complang (cons ’Perl complang)) (PERL C++ LISP JAVA COBOL) > complang (PERL C++ LISP JAVA COBOL)  Push/Pop - Implement a stack data structure  Push - shortcut for adding elements permanently  Pop - shortcut for removing elements permanently > (push complang ’Fortran) (FORTRAN PERL C++ LISP JAVA COBOL) > (pop complang) (PERL C++ LISP JAVA COBOL

Programming in Lisp; Instructor: Alok Mehta19 NthCdr, ButLast, Last  NthCdr - Generalization of Rest  Removes the first N elements; returns rest of list > (setf complang ’(C++ Java Lisp Cobol)) (C++ LISP JAVA COBOL) > (nthcdr 1 complang) ; same as (rest complang) (LISP JAVA COBOL) > (nthcdr 2 complang) ; same as (rest (rest complang)) (JAVA COBOL)  ButLast - Removes the last (n-1) elements > (butlast complang 2) (C++ LISP)  Last - Returns a list with all but the last element  This function is analogous to ‘first’ (note: returns a list though) > (last complang) (COBOL)

Programming in Lisp; Instructor: Alok Mehta20 Length, Reverse, Assoc  Length - Returns the number of top-level elements of a list > (length ’(1 2 (3 4) 5) 4  Reverse - Reverses the top level elements of a list > (reverse ’(1 2 (3 4) 5) (5 (3 4) 2 1); Note the positions of 3 and 4  Assoc - Searches sublists for an association (alist) > (setf sarah ’((height.54) (weight 4.4))) ((height.54) (weight 4.4)) > (assoc ’weight sarah) (weight 4.4)

Programming in Lisp; Instructor: Alok Mehta21 T, NIL, Symbols  You can’t reassign the following symbols T; True NIL; Empty List (also means false)  Symbols can include –letters, digits, + - * $ % ^ & _ = ~. > (setf ’funny_variable_name) FUNNY_VARIABLE_NAME

Programming in Lisp; Instructor: Alok Mehta22 Numbers  Lisp defines the following types of numbers  Integers (5, -3) –fixnum (implementation dependent), bignum  Ratios (1/3 -- not the same as.333!) > (+ 1/3 1/3); returns 2/3  Floating-Point (3.25) –short, single, double, long (all are implementation dependent)  Complex –Format: (complex ) > (setf i (complex 0 1)) #C(0 1) > (* i i)

Programming in Lisp; Instructor: Alok Mehta23 Misc Math Functions –(+ x1 x2 …)Returns X1 + X2 + … –(* x1 x2 …), (- x1 x2), (/ x1 x2)Computes -, *, / –(float x)converts “x” to a floating point number –(round x)rounds a number to the closest whole integer –(max x1 x2 …)Returns the maximum of its arguments –(min x1 x2 …)Returns the minimum of its arguments –(expt x1 x2)Computes first argument (x1) raised to the power of the second argument (x2). –(sqrt x)Computes the square root of x –(abs x)Computes the absolute value of x

Programming in Lisp; Instructor: Alok Mehta24 Review  Lisp = List Processing  Data and Programs represented using Symbolic Expressions –Atoms, Lists (represented using box analogy or cons cells)  Interpreter functions (load, help, bye)  Misc. math functions (+, -, /, *, sqrt,...)  Assigning variables (setf)  List manipulation –cons, remove, first, rest, append, list –push, pop –second, third, …, tenth, nthcdr, butlast, last –length, reverse, assoc  T, NIL  Numbers (integers, ratios, floating point, complex)