Early in AI Research, There Was A Need For Symbolic Computing
Early in AI Research, There Was A Need For Symbolic Computing
(A(DEF)BC)
A B
Defining Functions in Scheme
• Form: (DEFINE (funct_name params) body)
– (DEFINE (square num) (* num num))
• so we can now use square as (square x) which returns x 2
– (DEFINE (second alist) (car (cdr alist)))
• notice that for the parameter, I used alist, this was merely to avoid
confusion since list is defined as a function
• if we do (second ’(a b c d)), it returns ’b
– (DEFINE (factorial n)
If n = 0, return 1, otherwise return
(cond ((eq? n 0) 1) n * factorial (n-1)
(T (* n (factorial (- n 1))))))
– (DEFINE (fib n)
(if (> n 2) (+ (fib (- n 1)) (fib (- n 2))) 1))
– (DEFINE (foo alist)
(cond ((null? alist) 0) What does this function do?
(T (+ (foo (cdr alist)) 1))))
More Scheme Functions
(DEFINE (factorial n)
(IF (= n 0) 1 (* n (factorial (- n 1)))))
Member will return T if the atm is found
(DEFINE (member atm lis) in the lis (list) and nil if it is not found
(COND
(member ’d ’(a b c d e f)) returns T
((NULL? lis) NIL)
(member ’d ’(a b e f j k)) returns nil
((EQ? atm (CAR lis)) #T)
(ELSE (member atm (CDR lis))))) Member in Lisp and Common Lisp
will return the list starting at atm and going
to the end of the list, how can we modify
Scheme’s member to do this?
(DEFINE (append lis1 lis2)
(COND ((NULL? lis1) lis2)
(ELSE (CONS (CAR lis1)
(append (CDR lis1) lis2)))))
Determine if the items in lis are all the same, such as ’(a a a) versus ’(a b a)
Common Lisp Data Structures
• struct – similar to C/C++
– (defstruct car year make model mileage)
– (setf mycar (make-car : year 2005 : model ’camry))
• make-car is automatically generated when use defstruct to define car
• defstruct also creates member accessing functions for each field as car-member
– (car-year mycar) returns 2005
– (car-mileage mycar) returns nil since that member has no value yet
• arrays – use make-array to generate an array
– (setf anarray (make-array ’(10 5)) – creates a 10x5 array and points anarray at it
– access the array using (aref arrayname arrayindex(es))
• as in (setf (aref anarray 3 2) 12) which sets anarray[3][2] = 12
– indices start at 0 as in C/Java
– arrays can be resized/reshaped with the old values retained
• strings – treated as arrays of chars
– can be assigned using make-array or directly as in (setf name “Frank Zappa”)
– accessed through aref and length
• classes – added to the language in the late 80s
– define a class through defclass and a method through defmethod, and an object
(instance) through make-instance
– common lisp classes are unlike other OOPLs because methods are not necessarily
tied to classes, but can be tied to specific objects, and there is no information hiding
mechanisms
• like C++, common lisp classes can have multiple parents
Conclusion
• Functional languages vs. imperative languages:
– variables/memory usage less visible making programming easier
• at least in some situations
– simpler syntactic structures to deal with (everything is a list)
– concurrency easier to design and implement
– interpreted nature makes large systems easier to build
– exploits recursion as much as possible, more so than imperative languages
• Uses of functional languages:
– mostly used in AI research
• Natural Language Understanding (easy parsing partially due to recursive nature)
• Expert Systems (easy rule format)
• Knowledge Representation (symbolic capabilities)
• Machine Learning (dynamic storage)
– used to teach functional programming
– used to implement EMACS, MACSYMA and some operating systems