Lisp: Representation of Data

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.
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.
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.
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.
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 Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
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.
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.
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.
Yu-Tzu Lin ( 林育慈 )
Digital Electronics Data Structures LISP
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,
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.
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.
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.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
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
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
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.
CSE S. Tanimoto More-Concepts - 1 More Programming Language Concepts Currying Lazy Evaluation Polymorphism.
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 S-Expressions: ATOMs
Defining Macros in Lisp
Modern Programming Languages Lecture 20 Fakhar Lodhi
Data Structures in Lisp
Chapter 15 – Functional Programming Languages
Lists in Lisp and Scheme
LISP A brief overview.
First Lecture on Introductory Lisp
J.E. Spragg Mitthögskolan 1997
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Scheme: Basic Functionality
CSE S. Tanimoto Explicit Function Application
Functional Programming Concepts
Lisp: Using Functions as Data
LISP A brief overview.
Functional Programming Concepts
Lisp: Representation of Data
Defining Macros in Lisp
Defining Functions with DEFUN
Abstraction and Repetition
Lisp: Using Functions as Data
Bindings, Scope, and Extent
LISP: Basic Functionality
Modern Programming Languages Lecture 18 Fakhar Lodhi
Functional Programming Concepts
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Lisp.
Defining Macros in Scheme
LISP primitives on sequences
Lists in Lisp and Scheme
Presentation transcript:

Lisp: Representation of Data Atoms: Symbols, numbers, functions, strings, arrays, structures. Conses: Lists, dotted pairs. Forms: functional forms, special forms, macro forms. Evaluation of functional forms. Special forms: QUOTE, SETQ, IF CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

Lisp S-Expressions: ATOMs Every Lisp object is either an ATOM or a CONS Symbols and numbers are kinds of atoms: X, APPLE, A-SYMBOL 1, 5.7, 3/5 Many other Lisp data objects are considered to be atoms (even strings and arrays are atoms!). CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

Lisp S-Expressions: CONSes (Again, every Lisp object is either an ATOM or a CONS) A CONS represents an association or pairing of two other Lisp objects. (A . B) (APPLE . RED) (PI . 3.14159) (X . (Y . Z)) CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

Lisp S-Expressions: Lists We define lists as follows: The symbol NIL is a list; it’s the empty list. This list is written in list notation as ( ) Any cons having the following structure is a list, (S1 . (S2 . ( ... (Sn . NIL) ... ) ) ) where each S1 is either an atom or a cons. This list is written in list notation as (S1 S2 ... Sn) CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation Examples of Lists > ’(a b c d e) (A B C D E) > () NIL > nil > ’() > ’(apple . (banana . (lime . nil))) (APPLE BANANA LIME) CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

Predicates That Identify Lists > (atom ’(a b c)) NIL > (atom ’x) T > (consp ’(a b c)) > (consp ’x) CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

List predicates (continued) > (listp ’(a b c)) T > (listp ’x) NIL > (consp ’()) ; NIL is not a cons. > (listp ’()) ; NIL is a list. > (consp ’(a . b)) > (listp ’(a . b)) ;note listp’s limitation. CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

Lisp Tries to Print Conses as Lists > ’(a . (b . c)) (A B . C) > ’(a . nil) (A) > ’((a . b) . (c . d)) ((A . B)C . D) > ’((nil . nil) . (nil . nil)) ((NIL)NIL) CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation Lisp Forms A form is a list whose first element is a symbol that names an operator. If the first element names a function, then the form is a functional form. > (+ 1 2 3) ; a functional form 6 > (functionp #’+) T > (setq x 5) ; a special form 5 > (functionp #’setq) ; Error! CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

Evaluation of Functional Forms A functional form is evaluted as follows: If there are any arguments in the form, then they are evaluated in left-to-right order. The number of arguments is compared with the number permitted by the function named by the first element of the form. If the number is compatible, then the function is applied to the values of the arguments. CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation QUOTE Unlike functional forms, special forms are not required to have their arguments evaluated. QUOTE is a special form that returns its argument unevaluated. > (quote (+ 1 2 3)) (+ 1 2 3) > (quote x) X > ’(+ 1 2 3) > ’x CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation SETQ SETQ is a special form that evaluates its second argument and assigns that value to the symbol which must be its first argument. > (setq x (+ 1 2 3)) 6 > (setq x (* x 3)) 18 > (setq y ’(+ 1 2 3)) (+ 1 2 3) > (setq y (rest y)) (1 2 3) > (setq y (first y)) 1 CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation

CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation IF IF is a special form that evaluates its first argument. If the result is NIL is skips its second argument and evaluates and returns its their element if any. If result of evaluating the first element was not NIL, it evaluates the second argument and returns that. > (setq x 10) 10 > (if (> x 2) (- x 1) (+ x 1)) 9 CSE 415 -- (c) S. Tanimoto, 2004 Lisp representation