4
TES3111/TIC3151 Artificial Intelligence Lab 01: Lisp Tutorial 1: Introduction Objectives 1. introduction to Lisp syntax 2. familiarisation with the Allegro Lisp IDE Activities Read through the following examples. Over the next couple of weeks you need to get familiar with programming concepts used here. Only a few new ideas are introduced but they are all (intentionally) under-explained, you need to experiment. You will also need to use the Lisp IDE. In Lisp the fundamental data structure is a list. Anything between round brackets (parentheses) is a list. Program statements also look like lists so to deal with data (rather than program statements) we put a single quote character before the opening bracket, so... '(the cat sat on the mat) ...is a data list containing 6 words (symbols). Lisp evaluates everything you give it & prints the value it generates. The value of a data list is itself (see below). Note: in the code examples the > is used as the Lisp prompt. > '(the cat sat on the mat) user input (the cat sat on the mat) printed value lists can be nested to any level... > '(a (boring example of a) nested list) (a (boring example of a) nested list)

Lab01.doc

  • Upload
    asdfasf

  • View
    217

  • Download
    4

Embed Size (px)

Citation preview

Page 1: Lab01.doc

TES3111/TIC3151 Artificial Intelligence

Lab 01: Lisp Tutorial 1: Introduction

Objectives1. introduction to Lisp syntax2. familiarisation with the Allegro Lisp IDE

ActivitiesRead through the following examples. Over the next couple of weeks you need to get familiar with programming concepts used here. Only a few new ideas are introduced but they are all (intentionally) under-explained, you need to experiment. You will also need to use the Lisp IDE.

In Lisp the fundamental data structure is a list. Anything between round brackets (parentheses) is a list. Program statements also look like lists so to deal with data (rather than program statements) we put a single quote character before the opening bracket, so...

'(the cat sat on the mat)

...is a data list containing 6 words (symbols).

Lisp evaluates everything you give it & prints the value it generates. The value of a data list is itself (see below). Note: in the code examples the > is used as the Lisp prompt.

> '(the cat sat on the mat) user input(the cat sat on the mat) printed value

lists can be nested to any level...

> '(a (boring example of a) nested list)(a (boring example of a) nested list)

Program statements are also written in lists, typically the first list item is the name of a function and the other items in the list are arguments. "third" is a function which takes one argument (a list of symbols) and returns the third symbol in the list...

> (third '(the cat sat on the mat))sat

> (first '(the cat sat on the mat))the

> (rest '(the cat sat on the mat))(cat sat on the mat)

Page 2: Lab01.doc

other than lists, symbols and numbers can also be used as data. Symbols must be quoted, numbers do not need to be quoted...

> 'bananabanana

> 123123

> (+ 123 45)168

if you forget to quote a symbol, Lisp throws an error...

> bananaError: Attempt to take the value of the unbound variable `banana'.[condition type: unbound-variable]

errors also get thrown if functions are given inappropriate arguments...

> (third 'banana)Error: Attempt to take the cdr of banana which is not listp.[condition type: simple-error]

variables are bound in Lisp rather than assigned (don't worry about the difference between binding & assignment for now). Variables are introduced and given values using setf...

> mangoError: Attempt to take the value of the unbound variable `mango'.[condition type: unbound-variable]

> (setf mango '(cat rat frog bat))(cat rat frog bat)

> mango(cat rat frog bat)

> (third mango)frog

> (setf mango 75)75

> mango75

Page 3: Lab01.doc

TES3111/TIC3151 Artificial Intelligence

Lab 01: Symbolic Expressions and LISP Data

Q. 1 Write a Lisp expression for each of the following expressions:

1. 10.0 / (1 + 14 * 2)

(/ 10 (+ 1(* 14 2)))

2. the maximum of the numbers 12, 3, 8, 15, 9. 3. test if 2*3.14 is greater or equal to 13/2.

(>= (* 2 3.14) (/ 13 2))

4. a list containing 5 names of trees.

‘(durian apple mango banna paya) or (list ‘durian ‘apple ‘…)

5. the first element of the list above by a function call

(first var) (car var) (nth 0 var)

6. a function call resulting in a string telling us how many minutes are in a day. The minutes should be computed as a multiplication directly in the expression.