CS 470    ARTIFICIAL INTELLIGENCE     Instructor: Jeffrey Horn                    NMU
  (last updated Feb.17, 2006)

TIPS on SCHEME    (for Homework 1)


General SCHEME Programming:

Basic lisp functions for manipulating lists:

; (CONS element list) will put the element "a" onto the front of the list
; "list" and returns the result. For example, the following line returns
; the list "(a b c d)"

(cons 'a '(b c d))

; (CAR list) is like the opposite of cons.  It returns the first element
; of list.  Thus the following code returns "a"

(car '(a b c d))

; (CDR list) will return the rest of list after the first element has
; been removed. Thus the following will return the list "(b c d)"


(cdr '(a b c d))

; Putting it all together, convince yourself that the following code
; takes apart and puts together list, effectively doing nothing!

(cons (car list) (cdr list))

;  Finally, a few shortcuts...

(caar '((a b) (c d) (e f)))  ;  Short for "(car (car list))".  Yields "a" here.

(cdddr '(a b d e f))         ;  Short for "(cdr (cdr (cdr list)))".  Yields "(e f)" here.

(cadr '((a b) (c d) (e f)))  ;  Short for "(car (cdr list))".  Yields "(c d)" here.

(caadr '((a b) (c d) (e f)))  ;  Short for "(car (car (cdr list)))".  Yields "c" here.

;  and so on...

 

Below are some useful little functions.   Member? can be used to find duplicate states in a list of visited states.  Note how recursion is so natural in Scheme/Lisp (or perhaps you could say that iteration is a pain!).  Not always super efficient, but always elegant and concise!

;  (ELEMENT index L) returns the element at position "index" in list "L"

(define (element index L)
        (if (equal? index 1)(car L)
                            (element (- index 1) (cdr L) )
        )
 )

(define (empty? L) (null? L))
 

;  (MEMBER? element list) returns true (#t) if "element" is in "list" 
(define (member? element list)
        (if (equal? (length list) 0)
             #f
             (or (equal? element (car list))
                 (member? element (cdr list))
              )
         )
 )

 


Manipulating Variables (can be used in HW1):
 

Although it is not in the spirit of "functional programming", here is a way to define a global, external variable that you might want to use to store the list of visited states. Note that it is called into existence the moment you click "Run") (in Dr. Scheme) and invoke the interpreter.  Therefore it will stay around all the time the interpreter is running, keeping old data around in between calls to "solve".  So you'll need to reset it to the empty string '() each time solve is called.  You can use the "set!" command for that.  See below. 

define history '() )
 

; Solve:  the main function to call. Given an initial and a goal state, solve
; returns a plan to change the initial to the goal, if possible. It
; calls search to do a breadth-first search of all possible successor
; states (offspring) of the initial state.


(define (solve initial goal)
    (
        (set! history '() )
        (search (list (list 'quote initial)) goal history))
     )
)