LISP A brief overview.

Slides:



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

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.
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.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
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.
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.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
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.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
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)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
Lisp: a history Developed by John McCarthy in the 1950’s. Developed by John McCarthy in the 1950’s. Only Fortran has higher “seniority” Only Fortran has.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
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.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
ISBN Chapter 15 Functional Programming Languages.
Common lisp A functional programming language. Useful URL:
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming in Scheme and Lisp.
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.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
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.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 Common Lisp. 2 Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion –Interpretive.
Comparative Programming Languages Functional programming with Lisp/Scheme.
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,
Functional Programming Languages
Section 15.4, 15.6 plus other materials
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Chapter 15 – Functional Programming Languages
Getting Started with Lisp
Lists in Lisp and Scheme
LISP A brief overview.
Using Lisp Lisp is a interactive system
First Lecture on Introductory Lisp
The Metacircular Evaluator
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Modern Programming Languages Lecture 20 Fakhar Lodhi
The Metacircular Evaluator (Continued)
Lisp and Scheme I.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Lisp.
LISP primitives on sequences
Presentation transcript:

LISP A brief overview

Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy use of recursion Prefix notation of expressions Interpretive language Why Lisp It is the most widely used AI programming language It is good for writing production software It is especially good for prototyping It has got lots of features other languages don’t You can write new programs and extend old programs really, really quickly in Lisp

Common Lisp (de facto industrial standard) Variations Frantz Lisp (80’s) Common Lisp (de facto industrial standard) Common Lisp at UMBC (from CMU) At both gl.umbc.edu and cs.umbc.edu command line: clisp Related links Lisp online: http://www.csee.umbc.edu/331/resources/lisp/onLisp/ http://www.delorie.com/gnu/docs/emacs/cl_toc.html#SEC_Contents Lisp tutorial

Valid objects (S-expressions) Atoms: numbers: (real 1.0, integer 1) symbols: a consecutive sequence of characters (no space) e.g., a, x, price-of-beef. two special symbols: T and NIL for logical true and false. strings: a sequence of characters bounded by double quotes e.g., "this is red". (Note: LISP is case insensitive) Lists: a list of atoms and/or lists, bounded by "(" and ")" (a b c), (a (b c)) top elements of a list example: top elements of list (a b c) are a, b, and c top elements of list (a (b c)) are a and (b c) nil: empty list (same as ()).

2. Function calls also a list use prefix notation: (function-name arg1 ... argn) returns function value for the given list of arguments functions are either provided by Lisp function library or defined by the user. Examples: >(+ 1 3 5) 9 >(/ 3 5) 3/5 >(/ 3.0 5) 0.59999999999999998 >(sqrt 4) 2

3. Evaluation of S-expression 1) Evaluate an atom. numerical and string atoms are evaluated to themselves; symbols evaluate to their values if they are assigned values, return Error, otherwise; The values of T and NIL are themselves. 2) Evaluate a list - evaluate every top element of the list as follows, unless explicitly forbidden: the first element is always a function name; evaluating it means to call the function body; each of the rest of the elements will then be evaluated, and their values returned as the arguments for the function. Examples >(+ (/ 3 5) 4) 23/5 >(+ (sqrt 4) 4.0) 6.0 >(sqrt x) Error: variable X has no value

3) To assign a value to a symbol (setq, set) setq is a special form of function (with two arguments); the first argument is a symbol which will NOT be evaluated; the second argument is a S-expression, which will be evaluated; the value of the second argument is assigned to be the value of the first argument to forbid evaluation of a symbol (quote or ‘) >(setq x 3.0) 3.0 >x 3.0 >(setq y x) 3.0 ; the value of x is assigned as ; the value of y >y 3.0 >(+ x y) 6.0

. to force an evaluation, using function "eval" >(quote x) x >'x x >(setq z 'x) x . to force an evaluation, using function "eval" Two more assignment functions: (set x y) ; assign the value of y to the value of x. x is evaluated ; first and whose value must be a symbol ; "setq" is a combination of "set" and "quote" (setf x y) ; similar to but more general than "setq" in that x can be ; something other than a symbol. >(+ x z) Error: X is not of a NUMBER ... >(+ x (eval z)) 6.0

4. Basic LISP functions 1) list operations: car and cdr (nth i L) returns the ith top element of L (the front element is considered 0th element) >(setq L '(a b c)) (a b c) ; assigns a list (a b c) as the value of L >(car L) A ; returns the first top level element of list L >(cdr L) (B C) ; returns the rest of list L >(cddr L) (C) >(caddr L) C >(cadr L) B ; car of cdr of L >(cdddr L) NIL >(cadddr L) NIL >(nth 2 L) C

other list operations >(cons 'x L) ; insert symbol x at the front of list L (X A B C) >(list 'a 'b 'c) ; making a list with the arguments as its elements (A B C) ; if a, b, c have values x, y, z, then (list a b c) ; returns list (x y z) >(append '(a b) '(c d)) (A B C D) ; appends one list in front of another >(reverse L) ; reverses a list (C B A) >(length L) ; returns the length of list L 3

2) Predicates (a special function which returns NIL if the predicate is false, T or anything other than NIL, otherwise) =, >, <, >=, <= for numerical values; equal, eq for others (symbols, lists, etc.) tests if x is a atom tests if x is a list nil is both atom and list >(< x y) NIL >(= x y) T >(equal ‘x ‘y) NIL >(equal ‘a (car L)) T >(atom x) T >(atom L) NIL >(listp x) NIL >(listp L) T

also numberp, symbolp, null >(numberp ‘x) NIL >(numberp x) T >(symbolp ‘x) T >(symbolp x) NIL >(null L) NIL >(null NIL) T >(null x) NIL

3) Set operations (a list can be viewed as a set whose members are the top elements of the list) >(member 'b L) ; test if symbol b is a member (a top element) of L (B C) ; if yes, returns the sublist of L starting at the ; first occurrence of symbol b >(member ‘b (cons 'b L)) (B A B C) >(member x L) NIL ; if no, returns NIL >(union L1 L2) ; returns the union of the two lists >(intersection L1 L2) ; returns the intersection of the two lists >(set-difference L1 L2) ; returns the difference of the two lists ; (removing members of L2 from L1)

4) Conditional >(cond (<test-1> <action-1>) . (<test-k> <action-k>)) each pair (<test-i> <action-i>) is called a clause; if test-i (start with i=1) returns T (or anything other than NIL), this function returns the value of action-i; else, go to the next clause; usually, the last test is T, which always holds, meaning otherwise. cond can be nested (action-i may contain (cond ...))

5. Define functions heavy use of recursive definitions (defun func-name (arg-1 ... Arg-n) func-body) examples: set membership function recursive definition: x  L iff x = first(L)  x  rest(L) (defun member (x L) (cond ((null L) nil) ; base case 1: L is empty ((equal x (car L)) L) ; base case 2: x=first(L) (t (member x (cdr L))) ; recursion: test if x is in rest(L) ))

(defun intersection (L1 L2) (cond ((null L1) nil) ((null L2) nil) ((member (car L1) L2) (cons (car L1) (intersection (cdr L1) L2))) (t (intersection (cdr L1) L2)) )) Example: (intersection '(a b c) '(b a b c)) returns (a b c) (intersection '(b a b c) '(a b c)) returns (b a b c) (defun set-difference (L1 L2) ((null L2) L1) ((not (member (car L1) L2)) (cons (car L1) (set-difference (cdr L1) L2))) (t (set-difference (cdr L1) L2))

Define functions iteratively. (dolist (x L result) body) for each top level element x in L, do body(x); x is not equal to an element of L in each iteration, but rather x takes an element of L as its value; (dotimes (count n result) body) ; do body n times. count starts with 0, ends with n-1 Note: result is optional, to be used to hold the computing result. If result is given, the function will return the value of result, returns NIL, otherwise. (may change global variables as side effects.)

(defun sum2 (L) (setq y 0) (dotimes (count (length L) y) (setq y (+ y (nth count L))) )) (defun sum1 (L) (setq y 0) (dolist (x L y) (setq y (+ y x)))) >(setq L1 '(1 2 3)) (1 2 3) >(sum1 L1) 6 >(sum2 L1) 6

6. Other functions in LISP library 1) Predicates: zerop, plusp, evenp, oddp, integerp, floatp 2) Logical connector: and, or, not 5) Rounding: floor, ceiling, truncate, round 6) Others: max, min, abs, sqrt, 1+ (add 1), 1- (minus 1) (exp number) (base-e exponential) (expt Base-number Power-Number) (log number & Optional base-number) (isqrt number) Returns the greatest integer less than equal to the exact positive square-root of the number. (signum number) Returns -1, zero, or 1 according if the number is negative, zero, or positive.

7. Other features 1) Property lists: Assigning/accessing properties (attribute-value pairs) of a symbol To assign a property: (setf (get object attribute) value) To obtain a property: (get object attribute) Example: >(setq L2 ‘(a b c)) >(setf (get 'a 'height) 8) ; cannot use "setq" or "set" here 8 >(get 'a 'height) >(setf (get (cadr L2) 'height) 9) 9 >(get 'b 'height)

2) Associative list: attach a list of properties to a symbol, each property can be retrieved by key (property symbol) >(setf sarah '((height 6) (weight 100) (sex "F"))) ((HEIGHT 6) (WEIGHT 100) (SEX "F")) >(assoc 'weight sarah) (WEIGHT 100)

3) mapcar: (mapcar #'p-name L) transform list L to another list by performing procedure p-name to each top level element of L. >(mapcar #'sqrt L1) (1 1.4142135 1.7320508) transforming more than one lists (defun sq1 (x) (* x x)) define the function within mapcar (unnamed), use lambda notation >(mapcar #'sq1 L1) (1 4 9) >(mapcar #'set L2 L1) (1 2 3) >a 1 >(mapcar #'* L1 L1 L1) (1 8 27) >(mapcar #'(lambda (x) (set x (+ 1 (eval x)))) L2) (2 3 4) >a 2

4) input/output: print/read on screen: >(print (get 'a 'height)) 8 >(print L2) (A B C) >(setq p (read)) 10 ;typed on the screen 10 >p with external file: (with-open-file (<stream-name> <file-name> :direction :input or :output) ... ) internal variable name external file name

>(with-open-file (data "in.dat" :direction :input) ; input file “in.dat” contains (setq L3 nil) ; 1 2 3 4 5 (dotimes (count 5) (setq L3 (cons (read data) L3))) ) NIL >L3 (5 4 3 2 1) >(with-open-file (result "out.dat" :direction :output) (dotimes (count 5) (print (+ 1 (nth count L3)) result))) NIL ;an external file "out.dat" is created and contains 6 5 4 3 2

5) Some new primitive/functions Access a list first, second, ..., tenth ;extension of CAR, ;return the ith element rest, last ; extension of CDR, return a list Conditional (if <test> body1 body2) ;do body1 if test is true, ; body2, otherwise (when <test> body) ;do body when test is true (unless <test> body) ;do body when test is false

6) Miscellaneous %clisp ; enter Common Lisp of CMU (on gl.umbc.edu) or cs.umbc.edu) >(bye) or (quit) or <ctrl>-D ; exit CLISP (load "file-name") ; load in a file (ed "file-name") ; enter vi editor (compile-file "file-name") ; the compiled version is in file-name.o ; then load in file-name.o (compile 'func-name) ; compile a particular function (time (func-name arg1 ... argn)) ; print real and run time for executing func-name

Summary Atoms and lists Functions and function calls setq, setf, set, quote, eval, math functions (+, -, *, /, max, min, exp, sqrt, …) list operations: list, cons, car, cdr, length, nth, append, reverse predicates (=, >, equal, eq, numberp, symbolp, …) Defining functions (defun func_name (arg_list) func_body) dolist, dotimes, cond, if, when, unless, mapcar Properties and associative lists: get, assoc Input/output: print, read, with-open-file, load