LISP Lab 3: Optional Parameters & Functional Arguments in EEL5840 at University of Florida, Study notes of Electrical and Electronics Engineering

The notes for lisp lab 3 in the elements of machine intelligence course offered by dr. A. Antonio arroyo at the university of florida during the fall 2008 semester. The lab covers the topics of optional parameters and functional arguments in lisp. Students will learn how to define procedures with optional parameters using the &optional keyword and how to handle functional arguments using funcall and apply.

Typology: Study notes

Pre 2010

Uploaded on 03/10/2009

koofers-user-0mn-1
koofers-user-0mn-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sep-16-08—2:41 PM
1
1
University of Florida
EEL 5840 – Class #11 – Fall 2008
© Dr. A. Antonio Arroyo
EEL5840: Elements of Machine Intelligence
Rous Announcements
Announcements:
> None
Today’s Handouts in WWW:
> LISP Notes 2 & 3
> Outline Class 11
Web Site
> www.mil.ufl.edu/eel5840
> Software and Notes
> XLISP Documentation
2
University of Florida
EEL 5840 – Class #11 – Fall 2008
© Dr. A. Antonio Arroyo
EEL5840: Elements of Machine Intelligence
Rous Today’s Menu
LISP LAB 3
> Examples & Selected Homework 3 Solutions
> Optional Parameters in LISP
> Other Types of Parameters in LISP
> Functional Arguments
3
University of Florida
EEL 5840 – Class #11 – Fall 2008
© Dr. A. Antonio Arroyo
EEL5840: Elements of Machine Intelligence
Rous LISP Lab 2 - Examples
Are two SEXes equal?
(defun EQAL (Sex1 Sex2) (cond
((ATOM Sex1) (EQ Sex1 Sex2))
((ATOM Sex2) nil)
((EQAL (Car Sex1) (Car Sex2))
(EQAL (Cdr Sex1) (Cdr Sex2)))
))
4
University of Florida
EEL 5840 – Class #11 – Fall 2008
© Dr. A. Antonio Arroyo
EEL5840: Elements of Machine Intelligence
Rous LISP Lab 2 - Examples
What is the depth of a list?
(defun Depth (Lis) (Cond
((Null Lis) 0)
((Atom Lis) 0)
( t (max (+ (Depth (Car Lis)) 1)
(Depth (Cdr Lis)) ))
))
pf3
pf4
pf5

Partial preview of the text

Download LISP Lab 3: Optional Parameters & Functional Arguments in EEL5840 at University of Florida and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

1 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous (^) Announcements

  • Announcements: > None
  • Today’s Handouts in WWW: > LISP Notes 2 & 3 > Outline Class 11
  • Web Site > www.mil.ufl.edu/eel > Software and Notes > XLISP Documentation

2 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous (^) Today’s Menu

• LISP LAB 3

> Examples & Selected Homework 3 Solutions > Optional Parameters in LISP > Other Types of Parameters in LISP > Functional Arguments

3 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous LISP Lab 2 - Examples

Are two SEXes equal? (defun EQAL (Sex1 Sex2) (cond ((ATOM Sex1) (EQ Sex1 Sex2)) ((ATOM Sex2) nil) ((EQAL (Car Sex1) (Car Sex2)) (EQAL (Cdr Sex1) (Cdr Sex2))) ))

4 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous LISP Lab 2 - Examples

What is the depth of a list? (defun Depth (Lis) (Cond ((Null Lis) 0) ((Atom Lis) 0) ( t (max (+ (Depth (Car Lis)) 1) (Depth (Cdr Lis)) )) ))

5 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous (^) Hwk 3 - Selected Solutions

Unnest (defun unnest (lis) (cond ((null lis) lis) ( (atom (car lis)) (cons (car lis) (unnest (cdr lis)))) ( t (append (unnest (car lis)) (unnest (cdr lis))))))

-or- (defun unnest (lis) (cond ((null lis) nil) ((atom lis) (list lis)) (t (append (unnest (car lis)) (unnest (cdr lis)) ))) ))

6 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous (^) Hwk 3 - Selected Solutions

Setdiff (defun setdiff(s1 s2)(cond ((null s1) nil) ((member (car s1) s2) (setdiff (cdr s1) s2)) (t (cons (car s1) (setdiff (cdr s1) s2))) ))

7 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous Hwk 3 - Selected Solutions

EQSet (defun eqset(s1 s2) (equal (setdiff s1 s2) (setdiff s2 s1)))

Makeset (defun makeset(lis)(cond ((null lis) nil) ((member (car lis) (cdr lis)) (makeset (cdr lis))) ( t (cons (car lis) (makeset (cdr lis)))) ))

8 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous LISP Lab 3

  • Optional Parameters in LISP We can elegantly define procedures that include optional para- meters in LISP by using the &OPTIONAL keyword in the argu- ment list of a function definition. By enclosing the parameter(s) that follow the &optional keyword in parentheses one can supply a default value (otherwise they are automatically defaulted to nil). More than one parameter can follow the &optional keyword.
  • template : (defun fname (arg1 arg2 &optional arg3-argn) () )
  • Example: Write a function to count the top-level elements of a list (defun count-top (lis &optional (answer 0)) (if (endp lis) answer (count-top (cdr lis) (+ 1 answer)) ))

13 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous (^) LISP Lab 3

> (defun union2 (S1 S2 &optional (Ans)) (cond ( (null S1) (append Ans S2)) ( (member (car s1) S2) (union2 (cdr S1) S2 Ans)) ( 'else (union2 (cdr s1) S2 (append Ans (list (car S1))))) )) UNION > (union2 '(a b) '(c d)) (A B C D) > (union2 '(a) '(a b c d)) (A B C D) > (union2 '(a b) '(b c a)) (B C A)

14 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous (^) LISP Lab 3

  • Other Types of Parameters in LISP The &REST parameter in LISP is bound to a list of all otherwise accounted for argument values. Using this we could define a multiple list append as follows: > (defun manyappend (&rest lists)(app-h lists)) MANYAPPEND > (defun app-h (lists) (if (endp lists) nil (append (car lists) (app-h (cdr lists))) )) APP-H > (manyappend nil) NIL > (manyappend nil '(a b)) (A B) > (manyappend '(a b) '(c d) '(d e)) (A B C D D E) > (manyappend '(a b) nil '(c d)) (A B C D)

15 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous LISP Lab 3

  • Keyword parameters are designated by &KEY to allow for the easy identification of many parameters and to bypass positional binding. Using this we can write two versions of member, one using EQ the other using EQUAL. (defun MYMEMBER (SEX LIS &KEY TESTFCN) (COND ((NULL LIS) NIL ) ((IF (NULL TESTFCN) (EQ SEX (CAR LIS)) (TESTFCN SEX (CAR LIS))) LIS ) ( T (MYMEMBER SEX (CDR LIS) :TESTFCN TESTFCN)) )) > (mymember 'a '(a b)) (A B) > (mymember '(a) '((a) (b)) :testfcn 'equal) Error: The function TESTFCN is not defined. Happened in: # 16 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous LISP Lab 3

  • Using funcall we can fix MYMEMBER as follows: (defun mymember (sex lis &key testfcn) (cond ((null lis) nil ) ((if (null testfcn) (eq sex (car lis)) (funcall testfcn sex (car lis))) lis ) ( t (mymember sex (cdr lis) :testfcn testfcn)) ))

> (mymember '(a) '((a) (b))) NIL > (mymember '(a) '((a) (b)) :testfcn 'equal) ((A) (B)) > (mymember '(a) '((a) (b)) :testfcn 'eq) NIL

17 EEL 5840 – Class #11 – Fall 2008^ University of Florida © Dr. A. Antonio Arroyo

EEL5840: Elements of Machine Intelligence

Rous

Continue with

LISP Notes 2

The End!