Wk 13 Ses 35-37 LISP List Manipulation Commands

6
Artificial Intelligence LISP List Manipulation Commands * Property of STI Page 1 of 6 TOPIC TITLE: LISP LIST MANIPULATION COMMANDS Specific Objectives: At the end of the topic session, the students are expected to: Cognitive: 1. Recognize the various list manipulation commands. 2. Compare CONS, LIST, and APPEND. Affective: 1. Listen to others with respect. 2. Actively participate in class discussions. Psychomotor: 1. Combine car and cdr with con, list, and append. 2. Use all list commands to attain results. MATERIALS/EQUIPMENT: o Topic slides o OHP TOPIC PREPARATION: o Prepare the exercise to be answered by the students. o Provide additional examples and exercises on the topic presented. 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 LISP list manipulation commands. This will be the suggested flow of discussion for the course topic: 1. Discuss each list manipulation command. Provide additional example aside from the given in the slides. 2. Ask the students to answer the exercises provided in this topic.

description

hfuvvj

Transcript of Wk 13 Ses 35-37 LISP List Manipulation Commands

Page 1: Wk 13 Ses 35-37 LISP List Manipulation Commands

Artificial Intelligence

LISP List Manipulation Commands * Property of STI Page 1 of 6

TOPIC TITLE: LISP LIST MANIPULATION COMMANDS Specific Objectives: At the end of the topic session, the students are expected to: Cognitive:

1. Recognize the various list manipulation commands. 2. Compare CONS, LIST, and APPEND.

Affective:

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

Psychomotor:

1. Combine car and cdr with con, list, and append. 2. Use all list commands to attain results.

MATERIALS/EQUIPMENT:

o Topic slides o OHP

TOPIC PREPARATION:

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

presented. 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 LISP list manipulation commands. This will be the suggested flow of discussion for the course topic:

1. Discuss each list manipulation command. Provide additional example aside from the given in the slides.

2. Ask the students to answer the exercises provided in this topic.

Page 2: Wk 13 Ses 35-37 LISP List Manipulation Commands

Artificial Intelligence

LISP List Manipulation Commands * Property of STI Page 2 of 6

CONS Command Page 1 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 1 of 14

CONS Command

The CONS function takes two expressions and constructs a new list from them. If parameter B is not a list, then the result will be a ‘dotted-pair’.

SYNTAX:

PARAMETER A

PARAMETER B

Page 2 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 2 of 14

CONS Command

EXAMPLE:

(cons „a „b) (A B)

(cons „a nil) (A)

(cons „a „(b)) (A B)

(cons „(a b)„(c d)) ((A B)C D)

(cons (- 4 3)„(2 3)) (1 2 3)

CONS Command The most basic constructor of lists is CONS. Since it is a function, it evaluates both of its arguments. The second argument must be a list or NIL. Consider the examples below: (cons 1 nil)

(1)

(cons 2 (cons 1 nil))

(2 1)

(cons 3 (cons 2 (cons 1 nil)))

(3 2 1)

CONS adds a new item to the beginning of a list. The empty list is equivalent to NIL, () NIL. Thus, these could also be written as: (cons 1 ())

(1)

(cons 2 (cons 1 ()))

(2 1)

(cons 3 (cons 2 (cons 1 ())))

(3 2 1)

There is something special about NIL – it is one of the two symbols in LISP that is not a keyword, but it has itself as its constant value. The other symbol that works similar to NIL is T.

Since NIL evaluates to itself, combined with () NIL, you can write () instead of (QUOTE ()). Otherwise, LISP would have to make an exception to its evaluation rule to handle the empty list. [CONS Command, Pages 1-2 of 14]

LIST Command Page 3 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 3 of 14

LIST Command

The LIST function takes the expressions and constructs a list out of them. This constructed list is returned.

SYNTAX:

PARAMETER 1 to PARAMETER N

LIST Command Building a list out of nested CONS forms can be slightly tedious. The LIST form does the same thing in a more perspicuous manner. For example: (list 1 2 3)

(1 2 3)

LIST can take any number of arguments. Since LIST is a function, it evaluates its arguments. Consider the examples below: (list 1 2 :hello "world" 3)

(1 2 :HELLO “world” 3)

(let ((a :this)

(b :and)

(c :that))

(list a 1 b c 2))

(:THIS 1 :AND :THAT 2)

Page 3: Wk 13 Ses 35-37 LISP List Manipulation Commands

Artificial Intelligence

LISP List Manipulation Commands * Property of STI Page 3 of 6

Page 4 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 4 of 14

LIST Command

EXAMPLE:

(list) NIL

(list NIL) NIL

(list „a) (A)

(list „a „b) (A B)

(list „a „b NIL) (A B NIL)

In short, the LIST function will take any number of parameters and treat it as one big list. However, it will also observe its parameters if it involves any mathematical computation prior to conversion of the parameter to a list. [LIST Command, Pages 3-4 of 14]

APPEND Command Page 5 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 5 of 14

APPEND Command

The APPEND function takes an arbitrary number of lists and splices them together into a single list. This single list is returned.

If an empty list NIL is appended, it has no effect – it does not appear in the final list. (Remember that ‘(NIL) is not an empty list.) If an atom is appended, it also has no effect and will not appear in the final list.

SYNTAX:

PARAMETER 1 to PARAMETER N

Page 6 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 6 of 14

APPEND Command

EXAMPLE:

(append) (NIL)

(append „b) B

(append „(a) „(b)) (A B)

(append „(a) nil) (A)

(append (list „a „b) (list „c

„d)) (A B C D)

(append „(a (b)) „(c (d)))

(A(B)C(D))

(append „(a) nil nil nil „(b))

(A b)

(append „(a) „(nil) „(b)) (A

NIL B)

APPEND Command Although both CONS and APPEND may be used to combine smaller lists into a single list, it is important to note the difference between these two functions. If CONS is called with two lists as arguments, it makes the first of these a new first element of the second list, whereas APPEND returns a list whose elements are the elements of the two arguments. For example: (cons „(1 2) „(3 4))

((1 2) 3 4)

(append „(1 2) „(3 4))

(1 2 3 4)

The lists (1 2 3 4) and ((1 2) 3 4) have fundamentally different structures. This difference may be noted graphically by exploiting the isomorphism between lists and trees. APPEND can take in two or more parameters and converts them into one gigantic list. The basics of APPEND is that if the parameter(s) contains a list, it simply takes out that list. For example:

(a) a

(a b) a b

((a)) (a)

(a(b)) a (b)

[APPEND Command, Pages 5-6 of 14]

Page 4: Wk 13 Ses 35-37 LISP List Manipulation Commands

Artificial Intelligence

LISP List Manipulation Commands * Property of STI Page 4 of 6

REVERSE Command Page 7 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 7 of 14

REVERSE Command

The REVERSE function reverses the <list-expr>. The reverse list is the returned value.

The reversal process only occurs on the ‘top-level’ of the <list-expr>. If there are nested sublists, these are left intact.

SYNTAX:

Page 8 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 8 of 14

REVERSE Command

EXAMPLE:

(reverse NIL) (NIL)

(reverse „a) error: bad

argument type

(reverse „(a)) (A)

(reverse „(a b c)) (C B A)

(reverse „((a b) (c d) (e f) ))

((E F)(C D)(A B))

(reverse (list (+ 1 2)(+ 3 4)))

(7 3)

REVERSE Command The reversal of a list L is defined to be a list containing exactly the elements of L in reversed order. The Common LISP built-in function (reverse L) returns the reversal of L as: USER(1): (reverse '(1 2 3 4))

(4 3 2 1)

USER(2): (reverse '(1 (a b) (c d) 4))

(4 (c d) (a b) 1)

USER(3): (reverse nil)

NIL

Note: If the parameter is also a list, REVERSE does not reverse the inner list, but simply interchanges the position of the list. [REVERSE Command, Pages 7-8 of 14]

LAST Command Page 9 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 9 of 14

LAST Command

The LAST function returns a list containing the last node or element of a list. If the last node is a sublist, this is returned unaffected.

SYNTAX:

LAST Command The LAST function simply returns the last element in a given list parameter. For example: (last „(a b c))

(c)

However, this function MUST only accept list parameters else it would result to an error. [LAST Command, Pages 9-10 of 14]

Page 5: Wk 13 Ses 35-37 LISP List Manipulation Commands

Artificial Intelligence

LISP List Manipulation Commands * Property of STI Page 5 of 6

Page 10 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 10 of 14

LAST Command

EXAMPLE:

(last NIL) (NIL)

(last „a) error: bad argument

type

(last „(A)) (A)

(last „(A B C D E)) (E)

(last „(A (B C)(D E(F)))) ((D

E)(F))

MEMBER Command Page 11 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 11 of 14

MEMBER Command

MEMBER searches through <list-expr> for <expr>. If found, MEMBER returns the remainder of the <list-expr> starting with <expr>. Otherwise, a NIL is returned.

You may specify your own test with the :TEST and :TEST-NOT keywords followed by the test you perform.

SYNTAX:

(member <expr> <list-expr>

[{ :test | :test-not } <test> } ] )

Page 12 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 12 of 14

MEMBER Command

EXAMPLE:

(member „a „(1 2 3 4)) NIL

(member „2 „(1 2 3 4)) (2 3 4)

(setq mylist „(2 4 8 16 32 64

128 256))

(member „6 mylist :test „<) (8

16 32 64 128 256)

(member „6 (reverse mylist)

:test-not „<) (4 2)

(member ‟20 „(60 40 20 10) :test

„>) (10)

(member „(a) „((see) (a) (cat))

:test „equal) ((A)(C A T))

(member “hi” „(“a” “hi” “c”)

:test „string= ) (“hi” “c”)

MEMBER Command LISP defines a function (member E L) that returns non-NIL if E is a member of L. USER(64): (member 'b '(perhaps today is a good day to

live)) ; test fails

NIL

USER(65): (member 'a '(perhaps today is a good day to

live)) ; returns non-NIL

'(a good day to live)

This can be implemented in recursive version as: (defun list-member (E L)

"Test if E is a member of L."

(cond

((null L) nil)

((eq E (first L)) t)

(t (list-member E (rest L)))))

The correctness of the above implementation is easy to justify. The list L is either constructed by nil or by a call to cons:

Case 1: L is nil L is empty, and there is no way E is in L.

Case 2: L is constructed by cons Then it has two components: (first L) and (rest L). There are two cases, either (first L) is E itself, or it is not.

o Case 2.1: E equals (first L). This means that E is a member of L.

o Case 2.2: E does not equal (first L). Then E is a member of L iff E is a member of (rest L).

[MEMBER Command, Pages 11-12 of 14]

Page 6: Wk 13 Ses 35-37 LISP List Manipulation Commands

Artificial Intelligence

LISP List Manipulation Commands * Property of STI Page 6 of 6

CONS/LIST/APPEND Page 13 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 13 of 14

CONS/LIST/APPEND

Like CAR and CDR commands, CONS, LIST, and APPEND may also be combined with each other.

Example: List assumption (A B (C) (D E) F)

(A B)

• (list (car var) (cadr var))

((D E))

• (list (car(cdddr var)))

((C) . D)

• (cons (caddr var) (caar(cdddr

var)))

((F) A (D) E)

• (list (cddddr var) (car var)

(list (caar(cdddr var)))

(cadr(cadddr var)))

(C . C)

• (cons (caaddr var) (caar(cddr

var)))

CONS/LIST/APPEND Like CAR and CDR commands, it is also possible to combine CONS, LIST, and APPEND. Combining these commands along with CAR and CDR, you are now ready to create programs necessary for logic formulation. In the slide (see slide #), the example are already given. Take time to study these examples carefully and experiment with your own list to further understand CONS, LIST, and APPEND. [CONS/LIST/APPEND, Page 13 of 14]

Exercise Page 14 of 14

LISP List Manipulation Commands

Artificial Intelligence

* Property of STI

Page 14 of 14

Exercise

(setq numlist „((1 2) (3 4 5) (6 (7

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

(cdar numlist)

(cdadr numlist)

(cdddr numlist)

(caddr numlist)

(cdaddr numlist)

(+ (cadar numlist) (cadadr

numlist))

(> (caar numlist) (caaddr

numlist))

(cons (cadr numlist) (cdddr

numlist))

(cons (cdadr numlist) (cddadr

numlist))

(append (cddr numlist) (cdadr

numlist))

Exercise Note to the instructor: You may use this exercise as a quiz to let the students become familiar with CONS, LIST, and APPEND. It is also recommended that you should also produce additional materials for exercise. Call on students to place their answers on the board. Discuss the answer in class. [Exercise, Page 14 of 14]

EVALUATION:

o Ask the students to answer the exercise provided in this topic.

REFERENCES:

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

nd ed.), Prentice Hall

http://www.psg.com/~dlamkins/sl/chapter03-04.html http://www.cs.sfu.ca/CC/310/pwfong/Lisp/1/tutorial1.html http://www.cs.sfu.ca/CC/310/pwfong/Lisp/2/tutorial2.html