Wk 11 Ses 29-31 Intro to LISP

12
Artificial Intelligence Introduction to LISP * Property of STI Page 1 of 12 TOPIC TITLE: INTRODUCTION TO LISP Specific Objectives: At the end of the topic session, the students are expected to: Cognitive: 1. Familiarize with the concept of LISP. 2. Define what symbolic expression is. 3. Identify the types of symbolic expression. 4. Differentiate list from atom. 5. Learn the use of the SETQ command. 6. Familiarized with the car and cdr in manipulating list. Affective: 1. Listen to others with respect. 2. Actively participate in class discussions. Psychomotor: 1. Answer the exercises on using car and cdr. MATERIALS/EQUIPMENT: o Topic slides o OHP TOPIC PREPARATION: o Prepare the exercises to be answered by the students. o Provide additional examples and exercises on the topic presented. o Have the students research on LISP o It is imperative for the instructor to incorporate various kinds of teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to facilitate a thorough and creative discussion of the topic. o Prepare the slides to be presented in class. TOPIC PRESENTATION: The topic will revolve around the basic concepts of LISP. Discussions include overview of LISP, symbolic expressions, SETQ command, and basic list manipulation. This will be the suggested flow of discussion for the course topic: 1. Ask the students to have a brief discussion on what LISP is based on their research. Then introduce the topic on LISP as presented in the slides. Define what LISP is and discuss its history. 2. Discuss what symbolic expressions are. Then, explain its hierarchy. 3. Explain each category of Common LISP objects. 4. Discuss the common list formats and syntax rules. 5. Discuss what list and atoms are based on the given examples in the slides. 6. Explain the use of the SETQ command.

description

LISP

Transcript of Wk 11 Ses 29-31 Intro to LISP

Page 1: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 1 of 12

TOPIC TITLE: INTRODUCTION TO LISP Specific Objectives: At the end of the topic session, the students are expected to: Cognitive:

1. Familiarize with the concept of LISP. 2. Define what symbolic expression is. 3. Identify the types of symbolic expression. 4. Differentiate list from atom. 5. Learn the use of the SETQ command. 6. Familiarized with the car and cdr in manipulating list.

Affective:

1. Listen to others with respect. 2. Actively participate in class discussions.

Psychomotor:

1. Answer the exercises on using car and cdr.

MATERIALS/EQUIPMENT:

o Topic slides o OHP

TOPIC PREPARATION:

o Prepare the exercises to be answered by the students. o Provide additional examples and exercises on the topic

presented. o Have the students research on LISP o It is imperative for the instructor to incorporate various kinds of

teaching strategies while discussing the suggested topics. The instructor may use the suggested learning activities below to facilitate a thorough and creative discussion of the topic.

o Prepare the slides to be presented in class.

TOPIC PRESENTATION: The topic will revolve around the basic concepts of LISP. Discussions include overview of LISP, symbolic expressions, SETQ command, and basic list manipulation. This will be the suggested flow of discussion for the course topic:

1. Ask the students to have a brief discussion on what LISP is based on their research. Then introduce the topic on LISP as presented in the slides. Define what LISP is and discuss its history.

2. Discuss what symbolic expressions are. Then, explain its hierarchy.

3. Explain each category of Common LISP objects. 4. Discuss the common list formats and syntax rules. 5. Discuss what list and atoms are based on the given examples in

the slides. 6. Explain the use of the SETQ command.

Page 2: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 2 of 12

7. Discuss the use of car and cdr in manipulating list. 8. Ask the students to answer the exercises provided for this topic.

Overview of LISP Page 1 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 1 of 29

Overview of LISP

LISP

stands for List Processing

developed in 1959 and was the second oldest computer language in the world

typically interpreted, but compilers are available for some platforms

standard version was called Common Lisp

simplify the language and the results of these are another language called Scheme

most widely used with AI software

high-level programming language

Page 2 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 2 of 29

Overview of LISP

LISP was developed by John McCarthy at MIT and his goals were to:

Create a language for symbolic computation

Implement a model of computation based on the theory of recursion

Provide a clear definition of the language syntax and semantics

Demonstrate formally the completeness of this computational model

Page 3 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 3 of 29

Overview of LISP

In LISP, all expressions are built as parentheses-enclosed lists allowing programs to be handled as data.

Cogent’s Gamma language runs on top of SCADALisp – a version of LISP that has been optimized for size, speed and memory usage, and uses the strengths of LISP to facilitate development through:

Simplified programs

Unrestricted callbacks

Extensive GUI services

Run-time debugging

Powerful inter-task communications

Overview of LISP LISP, which stands for List Processing, is the second oldest computer language in the world. Developed in 1959, it lost the title to FORTRAN by only a few months. LISP is typically interpreted, but compilers are available for some platforms. There were attempts made to standardize the language and the standard version was called Common Lisp. There were also efforts to simplify the language and the results of these are another language called Scheme. LISP is a fairly portable language, but it is not particularly fast. It is most widely used with AI software. LISP is a high-level programming language. Most Emacs is written in the dialect of LISP called Emacs Lisp, which is extended with special features making it especially suitable for text editing tasks. LISP is considered to be powerful and easy to use programming language. It was developed by John McCarthy at MIT and his goals were to:

Create a language for symbolic computation Implement a model of computation based on the theory of

recursion Provide a clear definition of the language syntax and semantics Demonstrate formally the completeness of this computational

model

Today, LISP is used by most sophisticated programmers pushing the limits of computers in mathematical physics, computer-aided engineering, and computer-aided genetics. Also, it is used by thousands of people who do not consider themselves as programmers, only people who want to define shortcuts in AutoCAD or the Emacs text editor. In LISP, all expressions are built as parentheses-enclosed lists allowing programs to be handled as data. Cogent‟s Gamma language runs on top of SCADALisp – a version of LISP that has been optimized for size, speed and memory usage, and uses the strengths of LISP to facilitate development through:

Simplified programs Unrestricted callbacks Extensive GUI services Run-time debugging Powerful inter-task communications

LISP has a long, rich history way back forty years ago. It has survived all of the programming “revolutions” that have rendered lesser languages obsolete. In spite of being taught as a curiosity, if at all, by college and university staff who themselves have a poor understanding of the continuing growth and evolution of LISP, new generations of programmers continue to seek out LISP as a tool to solve some of the complicated problems in the world of computing. LISP can be very slow compared with any programming language. However, the difference may not be as large as you expect. Let us clear an old misconception about LISP. It is an interpreted language. And as a rule, most modern LISP systems compile to machine code. A few compile to byte code usually run five times slower than machine code.

Page 3: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 3 of 12

Page 4 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 4 of 29

Overview of LISP

LISP has a long, rich history way back forty years ago and it has survived all of the programming “revolutions” that have rendered lesser languages obsolete.

LISP can be very slow compared with any programming language.

Page 5 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 5 of 29

Overview of LISP

LISP is characterized by the following ideas:

Computing with symbolic expressions rather than numbers

Representation of symbolic expressions and other information by list structure in computer memory

Representation of information in external media mostly by multi-level lists and sometimes by S-expressions

A small set of selector and constructor operations expressed as functions

Composition of functions as a tool for forming more complex functions

The use of conditional expressions for getting branching into function definitions

The recursive use of conditional expressions as a sufficient tool for building computable functions

Page 6 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 6 of 29

Overview of LISP

Continuation…

The use of -expressions for naming functions

The representation of LISP programs as LISP data that can be manipulated by object programs

The conditional expression interpretation of Boolean connectives

The LISP function eval that serves both as a formal definition of the language and as an interpreter

Garbage collection as the means of erasure

Minimal requirements for declarations so that LISP statements can be executed in an on-line environment without preliminaries

LISP statements as a command language in an on-line environment

And one or two freeware LISP systems only run interpreted code, but they are the exception. Thus, there is part one of the answer: if you are not running a LISP compiler, you should get one. As a programming language, LISP is characterized by the following ideas:

Computing with symbolic expressions rather than numbers Representation of symbolic expressions and other information

by list structure in computer memory Representation of information in external media mostly by multi-

level lists and sometimes by S-expressions A small set of selector and constructor operations expressed as

functions Composition of functions as a tool for forming more complex

functions The use of conditional expressions for getting branching into

function definitions The recursive use of conditional expressions as a sufficient tool

for building computable functions

The use of -expressions for naming functions The representation of LISP programs as LISP data that can be

manipulated by object programs The conditional expression interpretation of Boolean

connectives The LISP function eval that serves both as a formal definition of

the language and as an interpreter Garbage collection as the means of erasure Minimal requirements for declarations so that LISP statements

can be executed in an on-line environment without preliminaries LISP statements as a command language in an on-line

environment Some of these ideas are new while others were taken from other languages. Towards the end of the initial period, it became clear that this combination of ideas made a well-designed mathematical system as well as a practical programming language. Mathematical neatness then became a goal and led to pruning some features from the core of the language. This was partly motivated by esthetic reasons and partly by the belief that it would be easier to devise techniques for proving programs correct if the semantics were compact and without exceptions. In 1960, John McCarthy published a paper in which he did for programming something like what Euclid did for geometry. He showed how, given a handful of simple operators and a notation for functions, you can build a whole programming language. He called this language LISP because one of his key ideas was to use a simple data structure called a list for both code and data. McCarthy‟s discovery was worth understanding, not just as a landmark in the history of computers, but as a model for what programming tends to become in our own time. It seems that there have been two really clean, consistent models of programming so far – the C and the LISP models. As computers have grown more powerful, the new languages being developed have been moving steadily toward the Lisp model. A popular technique for new programming languages in the past 20 years has been to take the C model of computing and add to it, piecemeal, parts taken from the Lisp model, like runtime typing and garbage collection.

Page 4: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 4 of 12

Page 7 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 7 of 29

Overview of LISP

In 1960, John McCarthy published a paper in which he did for programming something like what Euclid did for geometry.

McCarthy’s discovery was worth understanding, not just as a landmark in the history of computers, but as a model for what programming tends to become in our own time.

As computers have grown more powerful, the new languages being developed have been moving steadily toward the Lisp model.

Page 8 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 8 of 29

Overview of LISP

The LISP family of languages has evolved with the field of computer science, both by putting the best ideas from the field into practical use, and by contributing many such ideas.

In the late 1980s, many companies abandoned LISP in favor of other languages, starting the so called "AI winter".

In the early 1980s, there was a number of increasingly diverging LISP dialects.

The user community started a major consolidation and standardization effort to design the Common LISP dialect as a general purpose, industrial strength programming language.

The LISP family of languages has evolved with the field of computer science, both by putting the best ideas from the field into practical use, and by contributing many such ideas. The association of LISP with research, however, has not always been beneficial. LISP has always been among the main tools of AI since the beginning. When the commercial AI market failed to deliver on its promises, LISP was blamed as a scapegoat. In the late 1980s, many companies abandoned LISP in favor of other languages, starting the so called "AI winter". Although LISP survived the crisis, some of the resulting prejudice and lack of information is still present in the computing field. In the early 1980s, there was a number of increasingly diverging LISP dialects. The user community started a major consolidation and standardization effort to design the Common LISP dialect as a general purpose, industrial strength programming language. This work continued under the auspices of ANSI. When Common LISP was formally approved in 1994, it became the first ANSI standard (X3.226-1994) to incorporate object-oriented programming. [Overview of LISP, Pages 1-8 of 29]

Symbolic Expressions Page 9 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 9 of 29

Symbolic Expressions

Unlike most languages, LISP data serve not only as grist for programs, but also as building blocks for the language itself.

A LISP expression, a statement in the LISP language, is LISP data.

The most important type of LISP expression is called the symbolic expression, or s-expression.

An s-expression can be thought of as any expression from which LISP can derive some meaning or value.

There are two types of symbolic expressions: lists and atoms.

Symbolic Expressions Computer programs process many different types of data and most programming languages define explicit data types for programmers to work with. Unlike most languages, LISP data serve not only as grist for programs, but also as building blocks for the language itself. A LISP expression, a statement in the LISP language, is LISP data. This means that a LISP program is also data. This is why it is easy for LISP programs to analyze and create other LISP programs. The most important type of LISP expression is called the symbolic expression, or s-expression. All data objects that LISP can manipulate are called symbolic expressions. An s-expression can be thought of as any expression from which LISP can derive some meaning or value. There are two types of symbolic expressions: lists and atoms. Atoms and lists are hallmarks of the LISP language and there are literally hundreds of functions to facilitate their creation, modification, and manipulation. In LISP, all programs and program statements are lists. In this context, a list is defined to be an ordered set of zero or more elements surrounded on each side by parenthesis and separated by spaces. Each element in a list can either be a list or an atom.

Page 5: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 5 of 12

Page 10 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 10 of 29

Symbolic Expressions

In LISP, all programs and program statements are lists.

A list is an ordered set of zero or more elements surrounded on each side by parenthesis and separated by spaces.

Each element in a list can either be a list or an atom.

An atom, or atomic element, is the most primitive s-expression and is usually thought of as a variable.

Page 11 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 11 of 29

Symbolic Expressions

s-expression

atom

number

integer float

ratio

symbol

keyword

list

Page 12 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 12 of 29

Symbolic Expressions

Integer (int)

whole number that does not contain a decimal point; used for counting or where decimal points are not logical

valid range is from +2,147,483,647 to -2,147,483,648

Real (real)

number with a decimal point; used for numerical computation work with fractions, and the construction of point lists

stored using 8-byte double precision

String (str)

consists of a set of ASCII characters grouped together within a pair of double quote marks (“ ”)

used to generate prompts to the user, accept non-numeric data from the keyboard, and interface with data files

An atom, or atomic element, is the most primitive s-expression and is usually thought of as a variable. It should be noted that both numeric values, such as the numeric “5”, and symbolic text style names, such as "FOO", are both considered to be atoms. To illustrate, consider the expression (ONE (TWO 3) ((4 5) (6 7) (8 9))) to be a list. This list is comprised of three elements: the atom ONE, the list (TWO 3), and the list ((4 5) (6 7) (8 9)). This example shows that a list can be comprised of both atoms and lists, and that lists can be elements of other lists to create levels of list nesting. Special attention should be called to the empty list that is represented by (). An empty list is defined as a list that has no elements. In LISP, the atom NIL is tied to the empty list that can be interchangeable. The notion of an empty list is highly important in problem solving and is appropriately included in the language definition. The figure below illustrates a hierarchy of the most common types of s-expressions.

s-expression

atom

number

integer float

ratio

symbol

keyword

list

The most basic data element of LISP is the atom. In LISP, an atom is usually one computer word or a single computer byte. Data types that are more complex are built from atoms using lists. The LISP system uses predefined atomic types that permit easy manipulation of common data types. For instance, atomic types include integers and real numbers. With these atomic types, which made up of multiple computer words for higher precision, the mathematical expressions in LISP are easier to write.

Integer (int) – This is a whole number that does not contain a decimal point. This type is used for counting or where decimal points are not logical. The valid range for integer is from +2,147,483,647 to -2,147,483,648.

Real (real) – This is a number with a decimal point. It is used for numerical computation work with fractions, and the construction of point lists. A real number in LISP is stored using 8-byte double precision.

String (str) – This consists of a set of ASCII characters grouped together within a pair of double quote marks (“ ”). This type is used to generate prompts to the user, accept non-numeric data from the keyboard, and interface with data files.

Symbol (sym) – This represents lists, atoms, and functions defined in LISP. It is a pointer to other areas in memory where data are stored. The pointer used by LISP programmers is

Page 6: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 6 of 12

Page 13 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 13 of 29

Symbolic Expressions

Symbol (sym)

represents lists, atoms, and functions defined in LISP

pointer to other areas in memory where data are stored

Nothing (nil)

either an object has a value or empty

object having no value is considered as bound to nil

Function and List (list)

In LISP, a list is defined as a group of zero (nil) or more items contained within parentheses.

An atom such as a real number or a function symbol can be a single-list element.

Lists may contain both atoms and other lists in any combination or sequence.

Page 14 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 14 of 29

Symbolic Expressions

Lists in LISP are actually linked lists, which refer to the method of storing the lists.

An empty list is a special kind of list that does not contain anything inside it (no elements).

LISP uses the special symbol nil to mean empty. This means that, in the case of lists, nil and () mean the same thing.

Nil is absolutely unique in LISP in that it is the only datum in LISP that is both a symbol and a list.

Page 15 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 15 of 29

Symbolic Expressions

Numbers

provided in several forms and representations;

common Lisp provides a true integer data type: any integer, positive or negative, has in principle a representation as a Common Lisp data object, subject only to total memory limitations (rather than machine word width)

Characters

represent printed glyphs such as letters or text formatting operations

common Lisp provides for a rich character set, including ways to represent characters of various type styles

sometimes referred to as variable or function names, depending on the application of the symbol.

Nothing (nil) – This is either an object has a value or empty. An object having no value is considered as bound to nil.

Function and List (list) – In LISP, a list is defined as a group of zero (nil) or more items contained within parentheses. An atom such as a real number or a function symbol can be a single-list element. Lists may contain both atoms and other lists in any combination or sequence. Atoms and lists are collectively referred to as symbolic expressions.

A basic conceptualization of the internal workings of the LISP list structure helps understand the capabilities of this unique language. Lists in LISP are actually linked lists, which refer to the method of storing the lists. An empty list is a special kind of list that does not contain anything inside it (no elements). A list with no elements is written as a pair of parentheses ( ) with nothing in between (spaces do not matter). However, if you were to evaluate an empty list, the result would still be nil. Recall that LISP uses the special symbol nil to mean empty. This means that, in the case of lists, nil and () mean the same thing. But nil is a symbol so it has a dual nature (a symbol and a list). Nil is absolutely unique in LISP in that it is the only datum in LISP that is both a symbol and a list. The following are some categories of Common LISP objects that are of particular interest:

Numbers Characters Symbols Lists Arrays Structures Functions

Some of these categories have many subdivisions. The above categories are data types that are neither more nor less real than other data types. They simply constitute a particularly useful slice across the type hierarchy for expository purposes. Below are brief descriptions of these data types.

Numbers – These are provided in several forms and representations. Common Lisp provides a true integer data type: any integer, positive or negative, has in principle a representation as a Common Lisp data object, subject only to total memory limitations (rather than machine word width).

Characters – These represent printed glyphs such as letters or text formatting operations. Common Lisp provides for a rich character set, including ways to represent characters of various type styles.

Symbols – These are named data objects. LISP provides machinery for locating a symbol object, given its name (in the form of a string). Symbols have property lists, which in effect allow symbols to be treated as record structures with an extensible set of named components, each of which may be any LISP object.

Page 7: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 7 of 12

Page 16 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 16 of 29

Symbolic Expressions

Symbols

named data objects

have property lists, which in effect allow symbols to be treated as record structures with an extensible set of named components, each of which may be any LISP object

Lists

sequences represented in the form of linked cells called conses

Arrays

dimensioned collections of objects that can have any non-negative number of dimensions

indexed by a sequence of integers

can have any LISP object as a component

Page 17 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 17 of 29

Symbolic Expressions

Common List Formats:

Sequence• A sequence is a series, or linear ordering, of

elements. This is the most basic list format. Here is sequential list representing the twelve-tone row from Berg's Violin Concerto: (0 3 7 11 2 5 9 1 4 6 8 10).

Association• An association list is a list of lists. The first

element in each sublist is the name of the association and the rest of the elements in the sublist are the values to be associated with the name: ((row-forms p i r ri) (timbres rough sweet) (composers bach debussy stravisnski berg)).

Property• A property list is a sequence of pairs, where each

pair consists of a property name followed by a property value. This example demonstrates an implicit pairing of elements: (rhythm 1/4 hertz 440.0 marks (sfz fermata)). In this list, the symbol rhythm is paired with 1/4, hertz with 440.0, and marks with (sfz fermata).

Page 18 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 18 of 29

Symbolic Expressions

List syntax rules:

The left parenthesis ("opens" a list, the right parenthesis) "closes" it. Paired left and right parentheses are said to "match" or "balance".

Right parentheses close the nearest open left parentheses to its left.

Every left parenthesis must have a corresponding right parenthesis. A list with unbalanced parenthesis is undefined, that is, LISP does not accept it as a list.

Lists – These are sequences represented in the form of linked cells called conses. All other lists are built recursively by adding a new element to the front of an existing list. This is done by creating a new cons, which is an object having two components called the car (hold anything) and the cdr (made to point to the previously existing list).

Arrays – These are dimensioned collections of objects that can have any non-negative number of dimensions. These are indexed by a sequence of integers. A general array can have any LISP object as a component, while other types are specialized for efficiency and can hold only certain types of LISP objects.

Lists can be thought of as "databases" that associate or group information together. What the information is or how it is grouped is up to the programmer to decide. Although lists can represent a multitude of different organizations, there are three formats that are particularly common. These are the following:

A sequence is a series, or linear ordering, of elements. This is the most basic list format. Here is sequential list representing the twelve-tone row from Berg's Violin Concerto: (0 3 7 11 2 5 9 1 4 6 8 10).

An association list is a list of lists. The first element in each sublist is the name of the association and the rest of the elements in the sublist are the values to be associated with the name: ((row-forms p i r ri) (timbres rough sweet) (composers bach debussy stravisnski berg)).

A property list is a sequence of pairs, where each pair consists of a property name followed by a property value. This property list example demonstrates an implicit pairing of elements: (rhythm 1/4 hertz 440.0 marks (sfz fermata)). In this list, the symbol rhythm is paired with 1/4, hertz with 440.0, and marks with (sfz fermata).

Lists are beautiful. They are powerful, flexible and easy to work with. Their possible uses are limitless. But they can be a source of confusion for LISP beginners. Your ability to understand and program in LISP depends on your facility to understanding and notate lists. There are only three simple syntax rules to remember:

1. The left parenthesis ("opens" a list, the right parenthesis) "closes" it. Paired left and right parentheses are said to "match" or "balance".

2. Right parentheses close the nearest open left parentheses to its left.

3. Every left parenthesis must have a corresponding right parenthesis. A list with unbalanced parenthesis is undefined, that is, LISP does not accept it as a list.

[Symbolic Expressions, Pages 9-18 of 29]

Page 8: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 8 of 12

List/Atom Page 19 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 19 of 29

List/Atom

A list comprises of zero or more expressions.

Examples:

(A) This is a list that contains the element A.

(A B C) This is a list that contains the elements A B and C.

(AB C) This is a list that contains the element AB and C.

( ) This is a special list. It contains nothing. End result is NILL for empty or false.

EXPRESSIONS

The elements inside the list may not necessarily be numbers or letters but may also be a word or symbols.

Page 20 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 20 of 29

List/Atom

An atom may also be called as element. It may also be used as a variable for value storage.

Example:

A

Hello An atom may also be a word.

@ An atom may also be a symbol.

NOTE: A LIST may be an atom, but an atom may never be a LIST.

Page 21 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 21 of 29

List/Atom

A special list has two things to consider for it to become a special list:

If the list contains no elements, the result would be nil

Or it has a Boolean value inside the list

Examples:

( ) This is a list that contains noelement. The result in XLISP is NIL.

(t) This is a list that contains the Boolean value t for True.

(nil) This is a list that contains the Boolean value nil for False.

A result for false logical comparison.

The result for true in Xlisp. A result for correct logical comparison.

List/Atom LISP manipulates two types of parameters: list and atom. Each of these parameters has three different characteristics and knowing these characteristics is critical to produce programs that respond correctly to inputs. A list comprises of zero or more expressions. Consider the following examples of expressions: (A) This is a list that contains the element A. (A B C) This is a list that contains the elements A B and C. (AB C) This is a list that contains the elements AB and C. ( ) This is a special list that contains nothing. The end result is NIL for empty or false. The elements inside the list may not necessarily be numbers or letters, but can be also a word or symbols. An atom may also be called an element. It can also be used as a variable for value storage. For example:

Hello An atom may be a word

@ An atom may be a symbol Note that a list can be an atom, but an atom can never be a list. A special list is unique from an ordinary list. There are only two things to consider for it to become a special list and these are as follows:

If the list contains no elements, the result would be nil Or it has a Boolean value inside the list

Consider the examples below: ( ) This is a list that contains no element. The result in XLISP is NIL. (t) This is a list that contains the Boolean value t for True. (nil) This is a list that contains the Boolean value nil for False. As far as LISP is concerned, the value for false is nil and the value for true is t. [List/Atom, Pages 19-21 of 29]

Page 9: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 9 of 12

SETQ Command Page 22 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 22 of 29

SETQ Command

allows the user to declare variables as well as to store values on it

Xlisp is not very strict with its data typing –that is, a single variable may hold any data type

Syntax:

Command name Variable

name

Value to be stored

Page 23 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 23 of 29

SETQ Command

Examples:

(setq my_name “Juan”)

“Juan”

my_name

“Juan”

(setq a_var 57)

57

a_var

57

(setq a_var :a_key)

:A_KEY

SETQ Command LISP evaluates a symbol form by retrieving its variable value. SETQ allows you to set that value. For example: (setq my_name “Juan”)

“Juan” my_name

“Juan” (setq a_var 57)

57 a_var

57 (setq a_var :a_key)

:A_KEY The first argument of SETQ is a symbol. This is not evaluated. The second argument is assigned as the variable‟s value. SETQ returns the value of its last argument. SETQ does not evaluate its first argument because you want to assign a value to the symbol itself. If SETQ evaluated its first argument, the value of that argument would have to be a symbol. The SET form does this. In the given example, the variable var can be assigned with values using SETQ. You cannot use the equal (=) sign to assign values to a variable because this has special meaning in XLISP. Note the difference between the SET and SETQ forms in the following example. Make sure you understand what is happening with the (set var-1 99) form. (setq var-1 „var-2)

VAR-2 var-1

VAR-2 var-2

Error: Unbound variable (set var-1 99)

99 var-1

VAR-2 VAR-2

99 Notice the ‘ in the first form. This keeps the var-2 form from being evaluated. In the above example, we make the value of VAR-1 be the symbol VAR-2. Checking the value of VAR-2, we find that it has none. Then, we used SET to assign the value 99 to the symbol, VAR-2, which

Page 10: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 10 of 12

is the value of VAR-1. [SETQ Command, Pages 22-23 of 29]

Basic List Manipulation Page 24 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 24 of 29

Basic List

Manipulation

car list

returns the car of list, which should be a cons or () – that is, list should satisfy the predicate listp

if the cons is considered as the first cons of a list, then car returns the first element of the list

Ex: (car '(a b c)) => a

cdr list

returns the cdr of list, which should be a cons or () – that is, list should satisfy the predicate listp

if the cons is considered as the first cons of a list, then cdr returns the rest of the list, which is a list with all elements but the first of the original list

Ex: (cdr '(a b c)) => (b c)

Page 25 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 25 of 29

Basic List

Manipulation

Both car and cdr can also manipulate values of a variable.

Examples:

(setq var „(a b c))

(car var) result is A, same as (car ‘(a b c))

(cdr var) result is (B C)

(setq fruits ‘(apple banana

Cherry))

(car(cdr fruits)) result is banana

Page 26 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 26 of 29

Basic List

Manipulation

Example: Given a sample list, how do you get the middle part of the list? (setq var „(x y z) ) => (x y z)

(car (cdr var)) => y

Or

(cadr var) => y

What is the command to get z?

(car(cdr(cdr var))) or (caddr var)

NOTE: Allow only 3 consecutive car’s and cdr’s to make the command still valid. Furthermore, a max of 9 combination commands may be allowed.

Basic List Manipulation The following are the basic operations on conses viewed as pairs instead of constituents of a list. car list

This returns the car of list, which should be a cons or () – that is, list

should satisfy the predicate listp. By definition, the car of () is (). If

the cons is considered as the first cons of a list, then car returns the first

element of the list.

Example: (car '(a b c)) => a

cdr list

This returns the cdr of list, which should be a cons or () – that is, list

should satisfy the predicate listp. By definition, the cdr of () is (). If

the cons is considered as the first cons of a list, then cdr returns the

rest of the list, which is a list with all elements but the first of the original list.

Example: (cdr '(a b c)) => (b c)

Both car and cdr can also manipulate values of a variable. The slide

shows the combination of car and cdr commands (see slide #25).

Notice the command (car (cdr fruits)), this simply means that

the command car will manipulate whatever the cdr command returns.

The command (cdr fruits) returns (banana cherry), which was

passed to car, therefore the first element in that list is banana.

The slide shows that car and cdr can also be combined into 1

command, minimizing the use of parentheses (see slide #26). For example:

(car(cdr var)) is equal to (cadr var)

(car(cdr(cdr var))) is equal to (caddr var)

However, it should be noted that in order for the combination to work,

allow only 3 consecutive combinations of car‟s and cdr‟s. This means

that the command caaaaaaaaaar might not work.

[Basic List Manipulation, Pages 24-26 of 29]

Page 11: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 11 of 12

Exercises Page 27 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 27 of 29

Exercises

Assuming that you have a variable called var, create the shortcut version of the given command.

(car(cdr(car var)))

(cdr(cdr(cdr var)))

(cdr(car(car(cdr var))))

(car(car(cdr(cdr var))))

(car(cdr(cdr(car(car(cdr(car(car(cdr

var)))))))))

(cdr(cdr(cdr(cdr(car(car(car(car

var))))))))

(car(car(cdr(cdr(car(cdr(car var)))))))

Page 28 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 28 of 29

Exercises

(setq int „((1 2)(3 4(5 6(7)))(8 9)))

((1 2)(3 4(5 6(7)))(8 9))

(cadr int)

(cddr int)

(cdar int)

(cadar int)

(cdadr int)

(cdddr int)

(caddr int)

(cdaddr int)

(cddadr int)

(cadadr int)

Page 29 of 29

Introduction to LISP

Artificial Intelligence

* Property of STI

Page 29 of 29

Exercises

(setq list-1 „((a b c)(t nil)(d e(f g

h))))

((a b c)(t nil)(d e(f g h)))

(setq data-one „(1 2(3 4(5 6)(7 8))(9

10 11)))

(1 2(3 4(5 6)(7 8))(9 10 11))

(car list-1)

(cadr data-one)

(cddr (list-1))

(cddr list-1)

(cdaddr data-one)

(cdddr list-1)

(caddr data-one)

(cdddr data-one)

(caddr list-1)

(cadddr data-one)

Exercises Discuss each exercise to the class. You may provide additional exercises for the students. Also use the xlisp compiler to verify the answer of the students. [Exercises, Pages 27-29 of 29]

Page 12: Wk 11 Ses 29-31 Intro to LISP

Artificial Intelligence

Introduction to LISP * Property of STI Page 12 of 12

EVALUATION/GENERALIZATION:

o LISP stands for list processing. o The two basic types of symbolic expression are list and atom. o car and cdr are used in manipulating basic list. o Ask the students to answer the exercises provided in this topic.

REFERENCES:

Russell and Norvig, (2003), Artificial intelligence: a modern

approach (2nd

ed.), Prentice Hall http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node148.html