LISP primitives on sequences

Slides:



Advertisements
Similar presentations
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Advertisements

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.
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.
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 Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Common Lisp! John Paxton Montana State University Summer 2003.
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.
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.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
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.
CSE S. Tanimoto Lisp Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE,
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.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
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)
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
Functional Programming: Lisp MacLennan Chapter 10.
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
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.
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,
Lisp S-Expressions: ATOMs
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP LISt Processing.
LISP A brief overview.
First Lecture on Introductory Lisp
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Scheme: Basic Functionality
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Explicit Application of Procedures, and Explicit Evaluation
Lisp: Using Functions as Data
LISP A brief overview.
Lecture 13 - Assignment and the environments model Chapter 3
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Lisp: Representation of Data
Defining Functions with DEFUN
LISP LISt Processing.
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
LISP: Basic Functionality
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Programming Languages
Lisp.
Lisp: Representation of Data
The general format of case is the following: (case <key form>
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

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 the semester)) FIRST * (rest '(First day of the semester)) (DAY OF THE SEMESTER) First Rest A (B C) (A B C)

Examples * (first ( )) NIL * (rest ( )) * (first '((a b) (c d)) (A B) * (rest '((a b) (c d)) ((C D)) * (car '(a . b)) A * (cdr '(a . b)) B

The QUOTE mark ' stops evaluation * (first (rest '(a b c))) ;second returns B ;the 2nd element * (first '(rest (a b c))) ;third returns REST ;the 3rd element,... * (first (rest (a b c))) ;tenth returns ? ;the 10th element >>> Error:Undefined function: A while evaluating: (A B C) (#<COMPILED-FUNCTION 3:E196> ...) Debugger 1>

Examples * (first (first (rest '(rest ((a b)(c d)(e f)))))) (A B) * (first '(((a b) (c d) (e f)))) ((A B) (C D) (E F)) Given (apple (orange)((pear))(((kiwi))))), write a sequence of FIRST and REST which returns PEAR. * (first (first (first (rest (rest '(apple (orange) ((pear)) (((kiwi))))))))))))) PEAR * (rest '(apple (orange) ((pear)) (((kiwi))))))))) ((ORANGE) ((PEAR)) (((KIWI)))) * (rest '((orange) ((pear)) (((kiwi))))) (((PEAR)) (((KIWI)))) * (first (first (first '(((pear)) (((kiwi)))))))

The SETF / SETQ primitives The process of reserving a place in memory to store a value is called binding. The process of storing a value for a symbol is called assignment. The process of recovering a value from memory is called evaluation. SETF / SETQ assign values to symbols * (setf ab-list '(a b)) (a b) * ab-list * (setf ab-list '(a b) cd-list '(c d)) (c d) ; accepts multiple symbol-value pairs, ; but returns only the last assignment

SETF alters the contents of the cons cell storing the symbol. * (setf fact1 '(CS462 is a fun course)) (CS462 IS A FUN COURSE) * fact1 fact1 CS462 is a fun course

fact1 CS463 is a fun course * (first fact1) CS462 * fact1 * (setf (first fact1) 'CS463) CS463 is a fun course fact1 * fact1 (CS463 IS A FUN COURSE)

CONS, APPEND and LIST primitives construct lists * (cons 'a '(b c)) (A B C) FIRST REST CONS (A B C) A (B C) * (append '(a b c) '(x y z)) ;combines elements (A B C X Y Z) * (list '(a b c) '(x y z)) ;combines lists ((A B C) (X Y Z))

CONS, APPEND and LIST do not alter symbol values. * (append 'list1 'list2) ; does not accept atoms as arguments ERROR * (list 'list1 ' (x y z)) ; arguments can be both atoms and lists (LIST1 (X Y Z)) CONS, APPEND and LIST do not alter symbol values.

REST, NTHCDR, BUTLAST and LAST shorten lists * (rest '(a b c d)) (B C D) ; list without its first element * (nthcdr 2 '(a b c d)) (C D) ; list without ‘n’ first elements. * (nthcdr 4 '(a b c d)) NIL * (butlast '(a b c d) 2) (A B) ; list without ‘n’ last elements. * (butlast '(a b c d)) (A B C) ; list without its last element. * (last '(a b c d)) (D) ; list of just the last element. * (last '((a b) (c d) (e f))) ((E F))

More examples Add D at the end of the list (A B C) * (append '(a b c) (list 'd)) (A B C D) Add D at the beginning of the list (A B C) * (cons 'd '(a b c)) (D A B C) Add D as a second element in the list (A B C) * (append (list (first '(a b c))) (list 'd) (nthcdr 1 '(a b c))) (A D B C) Create a list of D and the last element of (A B C) * (setf new-list (list 'd (first (last '(a b c)))) ) (D C) * new-list

LENGTH counts the number of top-level elements, REVERSE reverses the order of top-level elements * (length '(a b ((c d) (e f)))) 3 * (length (append '(a b ((c d) (e f))) '(x y z))) 6 * (reverse '(a b ((c d) (e f)))) (((C D) (E F)) B A) * (reverse (append '(a b ((c d) (e f))) '(x y z))) (Z Y X ((C D) (E F)) B A)

User-defined procedures: the DEFUN primitive Build a list (a d) out of the list (a b c d). I way: Use the CONS primitive * (cons (first '(a b c d)) (last '(a b c d))) (A D) II way: Create a new procedure both-ends * (both-ends '(a b c d)) * (defun both-ends (whole-list) (cons (first whole-list) (last whole-list))) BOTH-ENDS

General form of the DEFUN primitive (defun <procedure name> (<parameter list>) <form 1> <form 2> … <form n>) DEFUN does not evaluate its arguments, it only establishes the procedure definition.

Example (cont.) * (setf whole-list '(a b c d)) (A B C D) * whole-list * (both-ends whole-list) (A D) (A B C D) ; the value was not affected by both-ends which used the same atom as argument.

LISP distinguishes between local (lexical) and special (global) variables Parameters in procedures are local variables. They are bound to argument value only inside the procedure. Values of global variables are set with the SETF primitive. Example: * (defun both-ends-global ( ) (setf whole-list (cons (first whole-list)(last whole-list)))) BOTH-ENDS-GLOBAL * whole-list (A B C D) * (both-ends-global) (A D)

Procedures may have any number of parameters * (defun both-ends-two-parameters (x y) (cons (first x) (last y))) BOTH-ENDS-TWO-PARAMETERS * (setf x '(a b) y '(c d)) (C D) * (both-ends-two-parameters x y) (A D)

Procedures may produce side effects * (defun both-end-with-side-effect (x y) (setf side-effect-1 '(This is a side effect)) (setf side-effect-2 '(Another side effect)) (cons (first x) (last y))) BOTH-END-WITH-SIDE-EFFECT * side-effect-1 Unbound symbol: SIDE-EFFECT-1 * side-effect-2 Unbound symbol: SIDE-EFFECT-2 * (both-end-with-side-effect x y) (A D) (THIS IS A SIDE EFFECT) (ANOTHER SIDE EFFECT)