Wk 14 Ses 38 Defining New Functions

7
Artificial Intelligence Defining New Functions Part 1 * Property of STI Page 1 of 7 TOPIC TITLE: DEFINING NEW FUNCTIONS Specific Objectives: At the end of the topic session, the students are expected to: Cognitive: 1. Define new functions in LISP. 2. Learn the use of the DEFUN command. 3. Differentiate IF from COND. Affective: 1. Listen to others with respect. 2. Actively participate in class discussions. Psychomotor: 1. Use the IF and COND statements in programming solutions. 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 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 defining new functions in LISP. This will be the suggested flow of discussion for the course topic: 1. Discuss the use of the DEFUN command and its examples. 2. Explain how the IF and COND statements are used in LISP and their differences. 3. Explain the sample programs presented in the slides. 4. Ask the students to answer the exercises provided in this topic.

description

Artificial Intelligence

Transcript of Wk 14 Ses 38 Defining New Functions

Page 1: Wk 14 Ses 38 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 1 * Property of STI Page 1 of 7

TOPIC TITLE: DEFINING NEW FUNCTIONS Specific Objectives: At the end of the topic session, the students are expected to: Cognitive:

1. Define new functions in LISP. 2. Learn the use of the DEFUN command. 3. Differentiate IF from COND.

Affective:

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

Psychomotor:

1. Use the IF and COND statements in programming solutions.

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 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 defining new functions in LISP. This will be the suggested flow of discussion for the course topic:

1. Discuss the use of the DEFUN command and its examples. 2. Explain how the IF and COND statements are used in LISP and

their differences. 3. Explain the sample programs presented in the slides. 4. Ask the students to answer the exercises provided in this topic.

Page 2: Wk 14 Ses 38 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 1 * Property of STI Page 2 of 7

DEFUN Command Page 1 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 1 of 14

DEFUN Command

The DEFUN function allows the user to create/define functions that perform specific task dictated by the user. The BODY are the statements that the function will perform.

SYNTAX:

Function Name

Page 2 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 2 of 14

DEFUN Command

EXAMPLE:

(defun addup (x y)

(+ x y)

)

The function addup will simply add two numbers represented by x and y. In this case, x and y are the parameters of the function addup. In order for this function to run, it is necessary for this function to accept two parameters…not one or three or more.

DEFUN Command The DEFUN command is used to define named function. (defun secret-number (the-number)

(let ((the-secret 37))

(cond ((= the-number the-secret) 'that-is-the-secret-number)

((< the-number the-secret) 'too-low)

((> the-number the-secret) 'too-high))))

SECRET-NUMBER

The DEFUN form has three arguments and these are as follows:

1. the name of the function: SECRET-NUMBER, 2. a list of argument names: (THE-NUMBER), which will be bound

to the function's parameters when it is called, and 3. the body of the function: (LET ...).

Since these three should stand for themselves, DEFUN does not evaluate any of its arguments. If it did, you will face the inconvenience of having to quote each argument. DEFUN returns the name of the defined function, and installs a global definition using the supplied name, parameter list, and body. Once a function using DEFUN is created, you can use it right away. For example: (secret-number 7)

TOO LOW

(secret-number 99)

TOO HIGH

(secret-number 37)

THAT-IS-THE-SECRET-NUMBER

When the function is called, its parameter (e.g., 99 in the second example) is bound to the argument name (i.e., THE-NUMBER) supplied in the definition. Then, the body of the function (i.e., (LET…)) is evaluated within the context of the parameter binding. In other words, evaluating (SECRET-NUMBER 99) causes the body of the SECRET-NUMBER function definition to be executed with the variable THE-NUMBER bound to 99. A function can be defined of more than one argument. For example: (defun my-calculation (a b c x)

(+ (* a (* x x)) (* b x) c))

MY-CALCULATION

(my-calculation 3 2 7 5)

92

[DEFUN Command, Pages 1-2 of 14]

Page 3: Wk 14 Ses 38 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 1 * Property of STI Page 3 of 7

IF Statement Page 3 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 3 of 14

IF Statement

The IF statement allows the user to test conditions and have different results depending on the condition stated.

SYNTAX:

Page 4 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 4 of 14

IF Statement

EXAMPLE:

(if (>= 3 4) ‘HI ‘HELLO)

(if (not(>= 3 4) ‘HI ‘HELLO)

(if (= 5 5) ‘HI ‘HELLO)

(if (not(= 5 5)) ‘HI ‘HELLO)

IF Statement All computer programming languages provide a means of testing the condition of an argument and then performing a sequence of steps based on the result of that test. In most computer programming languages, this is done with an IF THEN ELSE statement. LISP has its own version of this statement. Syntax: (if (condition) (TRUE part) (FALSE part)) The condition argument is the expression that is being tested and can be any of LISP data types. For example, to determine if the data supplied by the user is equal to 4, then either of the following test expressions can be used: (equal input 4) (= input 4) In both expressions if the variable input is equal to 4, then the expression returns the symbol T (true); otherwise the symbol nil (false) is returned. Once the condition has been evaluated, the result is then passed to the IF function. If the symbol T is passed, then the true part will take place otherwise, the false part will occur. [IF Statement, Pages 3-4 of 14]

COND Statement Page 5 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 5 of 14

COND Statement

The COND statement is used as an alternative for the IF statement. This statement is used when the IF statement becomes too many to manage.

SYNTAX:

COND Statement The COND macro lets you evaluate LISP forms conditionally. Like LET, COND uses parentheses to delimit different parts of the form. Consider these examples: (let ((a 1)

(b 2)

(c 1)

(d 1))

(cond ((eql a b) 1)

((eql a c) "First form" 2)

((eql a d) 3)))

2

In the above COND form, three clauses were defined. Each clause is a list beginning with a test form and followed by as many body forms as desired. The body forms are simply code that you want to execute if the test succeeds. The clauses are selected in order -- as soon as one test succeeds, the corresponding body forms are evaluated and the value of the last body form becomes the value of the COND form.

Page 4: Wk 14 Ses 38 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 1 * Property of STI Page 4 of 7

Page 6 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 6 of 14

COND Statement

EXAMPLE:

(cond (<= x 1) (print ‘Hi)

(<= x 7) ‘(print ‘Hello)

(t ‘(print ‘Bye))

)

COND is more general than the special form, IF, which only allows one test and one form each for the then and else parts. Conventional use of COND uses T as the test form in the final clause. This guarantees that the body forms of the final clause get evaluated if the tests fail in all of the other clauses. The last clause can be used to return a default value or perform some appropriate operation. For example: (let ((a 32))

(cond ((eql a 13) "An unlucky number")

((eql a 99) "A lucky number")

(t "Nothing special about this number")))

"Nothing special about this number"

[COND Statement, Pages 5-6 of 14]

Sample Program Page 7 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 7 of 14

Sample Program

EXAMPLE 1:

(defun TestProg (X)

(if (= x 1) (print ‘Hi))

(if (= x 2) (print ‘Hello))

(if (= x 3) (print ‘Hello))

)

Page 8 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 8 of 14

Sample Program

EXAMPLE 2:

(defun TestProg (X)

(cond (= x 1) (print ‘Hi)

(= x 2) (print ‘Hello)

(t (print ‘Bye))

)

)

Sample Program The sample program below will help the students understand better the nature of IF and COND and their differences when using those statements in LISP. Example 1: (defun TestProg (X)

(if (= x 1) (print ‘Hi))

(if (= x 2) (print ‘Hello))

(if (= x 3) (print ‘Hello)))

Example 2: (defun TestProg (X)

(cond (= x 1) (print ‘Hi)

(= x 2) (print ‘Hello)

( t (print ‘Bye))))

[Sample Program, Pages 7-8 of 14]

Page 5: Wk 14 Ses 38 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 1 * Property of STI Page 5 of 7

Reminder Page 9 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 9 of 14

Reminder

All programs are to be written using Notepad. Then save your files having .lsp as the file extension.

Reminder Remind the students that the text editor to be used for programming in LISP is Notepad. Make sure that the file is saved with a .lsp extension name. For example: “My_program.lsp” [Reminder, Page 9 of 14]

Exercises Page 10 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 10 of 14

Exercises

Create an XLisp program that will print the Grade of the student based on Average.

Average Grade

0 – 69 D

70 – 85 C

86 – 95 B

96 – 100 A

Page 11 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 11 of 14

Exercises

Solution:

(defun grade(ave)

(cond

((<= ave 69) (print 'D))

((<= ave 85) (print 'c))

((<= ave 95) (print 'B))

(t (print 'A))

)

)

Exercises Ask the students to answer the exercises presented in the slides (slides #10-14). It is advisable that these exercises should be done in the computer lab. Let the students solve first the problem before showing the actual solution. It is the instructors’ prerogative if they will consider these exercises as part of their quiz or a mere exercise to let the students understand further programming in LISP. [Exercises, Pages 10-14 of 14]

Page 6: Wk 14 Ses 38 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 1 * Property of STI Page 6 of 7

Page 12 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 12 of 14

Exercises

Create an XLisp program that would determine if the number is odd or even.

Solution:

(defun guess(x)

(if (= (rem x 2) 1) 'ODD 'EVEN)

)

Page 13 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 13 of 14

Exercises

Create a program that will convert Celsius to Fahrenheit.

Formula:

Fahrenheit = 9 * Celsius / 5 + 32

Solution:

(defun convert(celsius)

(setq ans (+ (/ (* 9 celsius) 5) 32))

(list "The answer is" ans)

)

Page 14 of 14

Defining New Functions

Artificial Intelligence

* Property of STI

Page 14 of 14

Exercises

Create a program that will compute for the area of a circle. Your input is the radius.

Formula:

Area = Pi r2

Solution:

(defun area(radius)

(setq ans (* 3.1416 (* radius

radius)))

(list "Answer is " ans)

)

Page 7: Wk 14 Ses 38 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 1 * Property of STI Page 7 of 7

EVALUATION:

o Ask the students to answer the exercises 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-07.html http://www.psg.com/~dlamkins/sl/chapter03-03.html