Wk 15 Ses 40-42 Defining New Functions

9
Artificial Intelligence Defining New Functions Part 2 * Property of STI Page 1 of 9 TOPIC TITLE: DEFINING NEW FUNCTIONS Specific Objectives: At the end of the topic session, the students are expected to: Cognitive: 1. Identify other printing functions in LISP. 2. Know the use of each printing function. Affective: 1. Listen to others with respect. 2. Actively participate in class discussions. Psychomotor: 1. Use printing functions of LISP in programming. 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 defining new functions in LISP. This will be the suggested flow of discussion for the course topic: 1. Discuss each printing function in LISP. Provide additional examples for each aside from what are presented in the slides. 2. Ask the students to answer the exercise provided for this topic. Discuss the answer in class.

description

AI

Transcript of Wk 15 Ses 40-42 Defining New Functions

Page 1: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

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

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

1. Identify other printing functions in LISP. 2. Know the use of each printing function.

Affective:

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

Psychomotor:

1. Use printing functions of LISP in programming.

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

1. Discuss each printing function in LISP. Provide additional examples for each aside from what are presented in the slides.

2. Ask the students to answer the exercise provided for this topic. Discuss the answer in class.

Page 2: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 2 * Property of STI Page 2 of 9

PROGN Page 1 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 1 of 20

PROGN

PROGN allows the user to perform several statements in a single list.

PROGN expression looks like this:

(progn

(goto-char ; First expression in progn.

(if (> arg 0) ; If arg is positive,

(1- (point)) ; move back one character;

(1+ (point)))) ; else move forward one

character.

(point)) ; Second expression in

progn:

; return position of point.

SYNTAX:

Page 2 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 2 of 20

PROGN

EXAMPLE:

(progn (car var)

(cdr var)

(cons (cdar var) (cadr var))

)

Page 3 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 3 of 20

PROGN

Other examples:

(progn) => NIL

(progn 1 2 3) => 3

(progn (values 1 2 3)) => 1, 2,

3

(setq a 1) => 1

(if a (progn (setq a nil) 'here)

(progn (setq a t) 'there)) =>

HERE

a => NIL

PROGN PROGN allows the user to perform several statements in a single list. Its body consists of two expressions. The PROGN expression looks like this: (progn

(goto-char ; First expression in progn.

(if (> arg 0) ; If arg is positive,

(1- (point)) ; move back one character;

(1+ (point)))) ; else move forward one

character.

(point)) ; Second expression in progn:

; return position of point.

In a PROGN expression, when the search is forward (arg is positive), Emacs leaves point just after the searched-for character. By moving point back one position, the character is uncovered. In this case, the above expression reads as follows: (goto-char (1- (point))). This moves point one character back. (The 1- function subtracts one from its argument, just as 1+ adds ones to its argument.) On the other hand, if the argument to zap-to-character is negative, the search will be backwards. The IF argument detects this and the expression reads: (goto-char (1+ (point))). (The 1+ function adds one to its argument.) The second and last argument to PROGN is the expression (point). This expression returns the value of the position to which point is moved by the first argument to PROGN. This value is then returned by the IF expression of which it is a part and is passed to kill-region as kill-region's second argument. In summary, the function works like this: the first argument to kill-region is the position of the cursor when the zap-to-char command is given--the value of point at that time. The search function then moves point if the search is successful. The PROGN expression moves point just enough so the zapped to character is not removed, and returns the value of point after all this is done. The kill-region function then removes the region. [PROGN, Pages 1-3 of 20]

Page 3: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 2 * Property of STI Page 3 of 9

PRINT Page 4 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 4 of 20

PRINT

The PRINT function simply prints any string and/or list variable. Its job is to represent output to the user.

Printing a self-referential object in the normal way would require an infinite amount of text, and the attempt could cause infinite recursion. Emacs detects such recursion and prints `#level' instead of recursively printing an object already being printed.

(setq foo (list nil))

(nil)

(setcar foo foo)

(#0)

Page 5 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 5 of 20

PRINT

Functions may leave some of their arguments unevaluated, as may be seen by the behavior of the PRINT statements in the following example.

(and (oddp 2) (print "second statement

was evaluated"))

nil

(and (oddp 3) (print "second statement

was evaluated"))

second statement was evaluated

(or (oodp 3) (print "second statement

was evaluated"))

t

(or (oddp 2) (print "second statement

was evaluated"))

second statement was evaluated

Page 6 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 6 of 20

PRINT

Function: print object &optional stream

It outputs the printed representation of object to stream, printing in addition one newline before object and another after it.

EXAMPLE:

(progn (print 'The\ cat\ in)

(print "the hat")

(print " came back"))

-|

-| The\ cat\ in

-|

-| "the hat"

-|

-| " came back"

-|

" came back"

PRINT The PRINT function simply prints any string and/or list variable. Its job is to represent output to the user. For a simple result such as 3, this is trivial. An expression which evaluated to a piece of list structure would require that print traverse the list and print it out as an s-expression. To implement a LISP REPL, it is necessary to implement only these three functions and an infinite-loop function. As expected, the implementation of EVAL will be complicated, since it must also implement all the primitive functions like CAR and + and special operators like IF. This done, a basic REPL itself is but a single line of code:

(loop (print (eval (read))))

LISP objects can refer to themselves. Printing a self-referential object in the normal way would require an infinite amount of text, and the attempt could cause infinite recursion. Emacs detects such recursion and prints

`#level' instead of recursively printing an object already being

printed. For example, `#0' indicates a recursive reference to the object

at level 0 of the current print operation: (setq foo (list nil))

(nil)

(setcar foo foo)

(#0)

Functions may leave some of their arguments unevaluated, as may be seen by the behavior of the PRINT statements in the following example. In addition to printing its argument, in some LISP environments PRINT returns a value of nil on completion. (and (oddp 2) (print “second statement was evaluated”))

nil

(and (oddp 3) (print “second statement was evaluated”))

second statement was evaluated

(or (oodp 3) (print “second statement was evaluated”))

t

(or (oddp 2) (print “second statement was evaluated”))

second statement was evaluated

Since oddp 2 evaluates to nil in the first expressions, the AND simply

returns nil without evaluating the PRINT form. In the second expression,

however, oddp 3 evaluates to T and the AND form then evaluates the

PRINT. A similar analysis may be applied to the OR example. It is important to be aware of this behavior, particularly if some of the arguments are forms whose evaluate have side effects, such as the PRINT function. The conditional evaluation of logical connectives makes them useful in controlling the flow of execution of LISP programs. In the function below, stream stands for an output stream. If stream is nil or omitted, it defaults to the value of standard-output. Function: print object &optional stream

The PRINT function is a convenient way for printing. It outputs the printed representation of object to stream, printing in addition one newline before object and another after it. Quoting characters are used. PRINT returns object. Consider the example below:

Page 4: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 2 * Property of STI Page 4 of 9

(progn (print 'The\ cat\ in)

(print "the hat")

(print " came back"))

-|

-| The\ cat\ in

-|

-| "the hat"

-|

-| " came back"

-|

" came back"

[PRINT, Pages 4-6 of 20]

PRIN1 Page 7 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 7 of 20

PRIN1

Function: prin1 object &optional stream

It does not print newlines to separate output as PRINT does, but it does use quoting characters just like PRINT.

EXAMPLE:

(progn (prin1 'The\ cat\ in)

(prin1 "the hat")

(prin1 " came back"))

-| The\ cat\ in"the hat"" came back"

" came back"

PRIN1

Function: prin1 object &optional stream

The PRIN1 function outputs the printed representation of object to stream. It does not print newlines to separate output as PRINT does, but it does use quoting characters just like PRINT. It returns the object as its value. Consider the example below: (progn (prin1 'The\ cat\ in)

(prin1 "the hat")

(prin1 " came back"))

-| The\ cat\ in"the hat"" came back"

" came back"

PRIN1 outputs the printed representation of object to output-stream. Escape characters are used as appropriate. The output from PRIN1 is

suitable for input to the function read.

(prin1 object output-stream)

== (write object :stream output-stream :escape t)

PRINT is similar to PRIN1 except that the printed representation of

object is preceded by a newline (see terpri) and followed by a space.

[PRIN1, Page 7 of 20]

Page 5: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 2 * Property of STI Page 5 of 9

PRINC Page 8 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 8 of 20

PRINC

Function: princ object &optional stream

This function is intended to produce output readable by people, not by read. Thus, it does not insert quoting characters and does not put double quotes around the contents of string.

EXAMPLE:

(progn (princ 'The\ cat)

(princ " in the \"hat\""))

-| The cat in the "hat“

" in the \"hat\""

PRINC

Function: princ object &optional stream

The PRINC function outputs the printed representation of object to stream and it returns the object as its value. This function is intended to

produce output readable by people, not by read. Thus, it does not

insert quoting characters and does not put double quotes around the contents of string. PRINC does not add any spacing between calls. Consider the example below: (progn (princ 'The\ cat)

(princ " in the \"hat\""))

-| The cat in the "hat"

" in the \"hat\""

PRINC is similar to PRIN1 except that the output has no escape characters. A symbol is printed as simply the characters of its print name; a string is printed without surrounding double quotes; and there may be differences for other data types as well. The general rule is that output from PRINC is intended to look good to people, while output from

PRIN1 is intended to be acceptable to the function read.

[PRINC, Page 8 of 20]

TERPRI Page 9 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 9 of 20

TERPRI

Function: terpri &optional stream

This function outputs a newline to stream.

The name stands for “terminate print.”

It is identical in effect to (write-char #\Newline output-stream); however, it always returns NIL.

TERPRI

Function: terpri &optional stream

The TERPRI function outputs a newline to stream. The name stands for

“terminate print.” It is identical in effect to (write-char #\Newline

output-stream); however, TERPRI always returns nil.

[TERPRI, Page 9 of 20]

Page 6: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 2 * Property of STI Page 6 of 9

READ Page 10 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 10 of 20

READ

The READ function lets the user enter a parameter instead of specifying the input immediately.

SYNTAX:

Page 11 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 11 of 20

READ

EXAMPLE 1: The program below lets the user specify a value for x and y. The function is flexible and easier to run since the input comes from the user.

(defun ADDUP ( )

(setq X (read ( ) ) )

(setq Y (read ( ) ) )

(+ X Y)

)

Page 12 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 12 of 20

READ

EXAMPLE 2: The program below is a better version of EXAMPLE 1. The PRINT function is used to let the user input values for x and y.

(defun ADDUP ( )

(print "Enter X: ")

(setq x (read() ) )

(print "Enter Y: ")

(+ x y)

)

READ The READ function reads the representation of an object instead of a single character. It will always signal an error, regardless of eof-error-p, if the file ends in the middle of an object representation. For example, if a file does not contain enough right parentheses to balance the left parentheses in it, READ will complain. If a file ends in a symbol or a number immediately followed by end-of-file, READ will read the symbol or number successfully and when called again will see the end-of-file and only then act according to eof-error-p. Likewise, the READ-LINE function will successfully read the last line of a file even if that line is terminated by end-of-file instead of the newline character. If a file contains ignorable text at the end, such as blank lines and comments, READ will not consider it to end in the middle of an object. Thus, an eof-error-p argument controls what happens when the file ends between objects. Syntax:

(setq X (read ()))

The sample program below lets the user specify a value for x and y. This time the function becomes flexible and easier to run since the input comes from the user. (defun ADDUP ( )

(setq X (read ( ) ) )

(setq Y (read ( ) ) )

(+ X Y)

)

The program below is a better version of the above code. The PRINT function is used to let the user input values for x and y. (defun ADDUP ( )

(print “Enter X: ”)

(setq x (read() ) )

(print “Enter Y: ”)

(+ x y)

)

It is better to provide a PRINT function first prior to the actual READ function. The purpose of the PRINT function is to tell the user what to do if it needs an interaction or input from the user. [READ, Pages 10-12 of 20]

Page 7: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

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

LAST Page 13 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 13 of 20

LAST

Function: last list

This function returns the last cons (not the last element) of list. If list is ( ), it returns ( ).

SYNTAX:

LIST PARAMETER

Page 14 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 14 of 20

LAST

EXAMPLE:

(last „(a b c)) (c)

(last „(a b)) ( b )

(last „(a)) ( a )

(last „a) error: bad argument

(last (reverse „(a b c))) (a)

LAST Function: last list

The LAST function returns the last cons (not the last element) of list. If list is ( ), it returns ( ). Syntax:

(last p)

For example: (setq x '(a b c d))

(last x) => (d)

(rplacd (last x) '(e f))

x => '(a b c d e f)

(last '(a b c . d)) => (c . d)

[LAST, Pages 13-14 of 20]

FIRST/SECOND/THIRD /FOURTH Page 15 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 15 of 20

FIRST/SECOND/THIRD

/FOURTH

Functions: first list

second list

third list

fourth list

SYNTAX:

(first/second/third/

fourth p)

FIRST/SECOND/THIRD/FOURTH LISP has a built-in feature that allows the programmer to extract elements of a list without using CAR and CDR. However, it is only limited up to four elements of the list.

Functions: first list second list third list fourth list

Syntax:

(first/second/third/fourth p)

Page 8: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 2 * Property of STI Page 8 of 9

Page 16 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 16 of 20

FIRST/SECOND/THIRD

/FOURTH

EXAMPLE:

(setq x „(a b (c) d e f))

(first x) a

(second x) b

(third x) ( c )

(fourth x) d

(fifth x) error: unbound

function

(first (third x)) c

These functions are sometimes convenient for accessing particular elements of a list. The FIRST function is the same as CAR, the SECOND is the same as CADR, and the THIRD is the same as CADDR, and so on. Note that the ordinal numbering used here is one-origin, as

opposed to the zero-origin numbering used by nth. For example:

(fourth x) == (nth 3 x)

[FIRST/SECOND/THIRD/FOURTH, Pages 15-16 of 20]

Nth Page 17 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 17 of 20

Nth

Function: nth n list

This function simply allows to return the nth element of a list.

SYNTAX:

LIST PARAMETERPOSITION OF ELEMENT

Page 18 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 18 of 20

Nth

EXAMPLE:

(setq x (list 1 2 3 4))

(first x) 1

(second x) 2

(third x) 3

(nth 2 x) 3

(list (first x) (second x) (nth

2 x)) (1 2 3)

(rest x) (2 3 4)

Nth

Function: nth n list

Syntax:

(nth num p)

(nth n list) returns the nth element of list, where the CAR of the list

is the ''zeroth'' element. The argument n must be a non-negative integer. If the length of the list is not greater than n, then the result is (), that is, nil. This is consistent with the idea that the CAR and CDR of () are each (). For example: (nth 0 '(foo bar gack))

foo

(nth 1 '(foo bar gack))

bar

(nth 3 '(foo bar gack))

()

Compatibility note: This is not the same as the Interlisp function called

nth, which is similar to but not exactly the same as the Common Lisp

function nthcdr. This definition of nth is compatible with Lisp Machine

Lisp and NIL (New Implementation of Lisp). Also, some people have

used macros and functions called nth of their own in their old MacLisp

programs, which may not work the same way.

nth may be used to specify a place to setf; when nth is used in this

way, the argument n must be less than the length of the list.

Note that the arguments to nth are reversed from the order used by

most other sequence selector functions such as elt.

[Nth, Pages 17-18 of 20]

Page 9: Wk 15 Ses 40-42 Defining New Functions

Artificial Intelligence

Defining New Functions – Part 2 * Property of STI Page 9 of 9

Exercise Page 19 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 19 of 20

Exercise

Create a LISP program that will let the user enter a number. It should be able to print out the square of the number entered. Call the function square(). The function is triggered by invoking square().

Page 20 of 20

Defining New Functions – Part 2

Artificial Intelligence

* Property of STI

Page 20 of 20

Exercise

Solution:

(defun square()

(print "enter_number:")

(setq x (read()))

(setq ans (* x x))

(list "answer is " ans)

)

Exercise This exercise will help the students further understand LISP as a whole. Create a LISP program that will let the user enter a number. Your program should be able to print out the square of the number entered.

Call the function as square(). The function is triggered by invoking

(square). Solution: (defun square()

(print "enter_number:")

(setq x (read()))

(setq ans (* x x))

(list "answer is " ans)

)

[Exercise, Pages 19-20 of 20]

EVALUATION:

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

REFERENCES:

http://sunsite.ualberta.ca/Documentation/Gnu/emacs-lisp-intro/html_node/emacs-lisp-intro_111.html

http://www.zvon.org/other/elisp/Output/SEC256.html http://en.wikipedia.org/wiki/Lisp_(programming_language) http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node198.html http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node195.html http://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node149.html