Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.

Slides:



Advertisements
Similar presentations
Introduction to LISP Programming of Pathway Tools Queries and Updates.
Advertisements

Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
1 Programming Languages and Paradigms Lisp Programming.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
TUTORIAL 3 CSCI3230 ( First Term) By Leo LIU Paco WONG CAO Qin 1 Hands on.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
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.
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:
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)
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
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.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Yu-Tzu Lin ( 林育慈 )
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
Functional Programming 02 Lists
PRACTICAL COMMON LISP Peter Seibel 1.
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
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.
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.
ITEC 380 Organization of programming languages Lecture 5 – Functional Programming.
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Basic Introduction to Lisp
Macros and general code walkers in Lisp: how useful! or, how useful? Ernst van Waning
Functional Programming: Lisp MacLennan Chapter 10.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
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
Lisp S-Expressions: ATOMs
Example of formula (defun roots (a b c) (list
LISP A brief overview.
J.E. Spragg Mitthögskolan 1997
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Lisp: Representation of Data
Defining Macros in Lisp
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
Common Lisp II.
Lisp.
Presentation transcript:

Introduction to LISP

Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs that write programs (macros)

Prefix notation >(+ 2 3) 5 >( ) 14 >(/ (- 7 1) (- 4 2)) 3

Prefix notation evaluation >(/ (- 7 1) (- 4 2)) 3 1.Arguments from left to right 2.Arguments are passed to the function

Data Integer > Strings >"hello world“ "hello world“ Symbols Symbols are words >’hello HELLO –Usually converted to uppercases –Lisp is case insensitive Lists

Quote >(quote (+ 3 5)) (+ 3 5) >(’ (+ 3 5)) (+ 3 5) quote is a special operator (distinct evaluation rule) Protects expressions from evaluation

Lists Lists are zero or more elements enclosed in parentheses >(list 3 2 1) (3 2 1) >’(3 2 1) (3 2 1) >(list '(+ 2 1) (+ 2 1)) ((+ 2 1) 3)

Lists Lisp programs are expressed as lists This is why we need the quote –Quoted list: returns the list itself –Unquoted list: trated as code and returns its evaluation

List operations >(cons ‘a ‘(b c d)) (A B C D) >(car ‘(a b c d)) A >(cdr ‘(a b c d)) (B C D) >(car (cdr ‘(a b c d))) B second, third, nth

Truth T, NIL, null, not >(listp ‘(a b c)) T (>listp 27) NIL >(null nil) T >(not nil) T numberp, listp, stringp, …

Conditionals (if (listp ‘(a b c)) (+ 1 2) (+ 5 6)) 3 (if (listp 27) (+ 2 3)) NIL

AND / OR (and t NIL) NIL (and t (+ 1 3)) 3 (or t NIL) T (or t (+ 1 3)) T

Functions (defun our-third (x) (car (cdr (cdr x))) >(our-third ‘(A B C D)) C

Recursion (defun our-member (obj lst) (if (null lst) nil (if (eql (car lst) obj) T (our-member obj (cdr lst)))))

Iteration (defun show-squares (start end) (do ((i start (+ i 1 ))) ((> I end) ‘done) (format t “~s ~s ~%” i (* i i)))) We do not always need recursion. When we want to do something repeatedly, iteration is sometimes more natural A typical case is to generate some sort of table or iterating over an array Prints out the squares of the integers from start to end

Recursive show-squares For comparison, here is a recursive version of show-squares. (defun show-squares (i end) (if (> i end) ‘done (progn (format t “~s ~s ~%” i (* i i)) (show-squares (+ i 1) end)))) Note: progn is an special operator that evaluates its arguments in order and returns the value(s) of the last.

Functional programming >(setf lst ‘(c a r a t)) (C A R A T) (remove ‘a lst) (C R T) Removes does not remove an object from a list (not literally) The function returns a new list containing everything on the original list but the object Use (setf x (remove ‘a x)) instead

Functions as objects In Lisp, functions are regular objects (like symbols or strings or lists) Like any other kind of object, we can pass functions as arguments. One example of a function that takes a function as parameter is apply >(apply #’+ ‘(1 2 3)) 6 #’ (sharp-quote) is an abbreviation for function just as ‘ is an abbreviation for quote >(apply (function +) ‘(1 2 3)) 6 The function funcall does the same thing but does not need the arguments to be packaged in a list: >(funcall #’ ) 6

Equality > (eql '(1 2) '(1 2)) NIL > (equal '(1 2) '(1 2)) T > (eql 1 1) T > (equal 1 1) T > (= 1 1) T > (= 1 1.0) T > (eql 1 1.0) NIL > (equal 1 1.0) NIL

Mapping functions > (mapcar #'sqrt '(4 9 16)) (2 3 4) Mapcar takes a function and a list and returns the result of applying the function to every element from the list. Other map functions –maplist, mapcan, mapcon, …

Some functions you might need car cdr defun if lambda list listp stringp loop mapcar not defvar setf setq numberp funcall remove defmacro

Some useful functions for the homework Instantiate the game class –(setq mypuzzle (make-instance 'game :board *easy-test* :name “easy”)) –Creates an instance of the class game and sets the att board to *easy- test* and the att name to “easy” –To access the atts (above we just set them when creating the instance), use the accessors defined in the defclass (game-board mypuzzle) (game-name mypuzzle)

Some useful functions for the homework Use the functions provided. Particularly row- groups, column-groups, and block-groups: (setq rows (ROW-GROUPS (game-board mypuzzle))) (setq cols (COLUMN-GROUPS (game-board mypuzzle))) (setq blocks (BLOCK-GROUPS (game-board mypuzzle))) game-board is the accessor to the board attribute in the game class You can print the lists: >(print rows) (( ) ( ) ( ) ( ))

Some useful functions for the homework (not (member t (mapcar 'duplicate-values rows))) no duplicate values on the rows of the board (rows is a list of lists as you can see on previous slide) (defun duplicate-values (lst) (has-duplicates (sort (remove '- lst) #'<))) has-duplicates should be a recursive function similar to our- member defined on previous slides. The logic is: –return NIL for lists of size 1 or less –return true if the first and second element are equal –call has-duplicates on the rest of the list

Some useful functions for the homework Removes the dashes from the list lst –(remove ‘- lst) Member which returns NIL if the list does not contain the element –> (member 1 '(2 3 4)) –NIL –(not (member t (mapcar 'duplicate-values rows))) no duplicate values on the rows of the board (rows is a list of lists as you can see on previous slide)

Some useful functions for the homework iota creates a list of numbers within a range > (iota 10 1) ( ) sort returns a the list sorted according to a comparison function > (sort '( ) #'<) ( ) To check if a list (e.g. a row, a column, a block) has all the values from 1 to MAXX (useful function for defining goalp): (defun all-values (lst) (equal (iota MAXX 1) (sort (remove '- lst) #'<) ))

From the code from the book Use utilities.lisp if you want to use the iota function You might want to use queue.lisp too (includes functions for queues maniulations) You might want to check simple.lisp for the general search (an idea of how to implement it)