;solve takes the initial state as its parameter ;It uses a helper function to solve the problem ;Then it prints out a solution of the problem as ; a list of states leading to the goal (nil if ; there no solution) ;Note: sm returns the solution in reverse order (defun solve (state) (reverse (sm state (list state) nil t))) ;sm is a helper function used to solve the problem ;Parameters are: ; - state (current state) ; - path (the path of states leading to the current ; state in reverse order with current state ; at beginning) ; - children (children of current state, which starts ; out as nil when you first arrive at a ; state, then the node is expanded, and ; this function is called recursively, each ; time using up one of the children. When it ; becomes nil again, all children have been ; checked to see if they lead to a solution) ; - first_time (a boolean value indicating whether ; you just arrived at current state) ;sm returns a sequence of states leading to a solution ; (in reverse order) if there is a solution, otherwise ; it returns nil (defun sm (state path children first_time) ; if state is a goal state, return solution (cond ((goal_test state) path) ; if just arrived at state and already been there, return nil ((and first_time (in state (cdr path))) nil) ; if just arrived at state, expand node and call recursively (first_time (sm state path (expand state operators) nil)) ; if all children already checked, return nill ((null children) nil) ; otherwise call sm recurively on first child in list, ; and pass what it returns to helper function sm2 (t (sm2 state children path (sm (car children) (cons (car children) path) nil t))))) ;sm2 is another helper function ;it has the same parameters as sm, except for: ; - tryone (result returned back from first child of list) ;It returns the same thing as sm (defun sm2 (state children path try_one) ; if call to child failed, call sm for rest of children (cond ((null try_one) (sm state path (cdr children) nil)) ; otherwise return solution (t try_one))) ;expand takes a state and a list of operations ; and returns all children of that state (defun expand (state ops) ; if no more operations, return nil (cond ((null ops) nil) ; otherwise append the result of appying first operator ; to the state, to the result of exanding the state with ; the rest of the operators (t (append (funcall (car ops) state) (expand state (cdr ops)))))) ;just a shortcut for member, because I hated writing that stuff ; at the end (defun in (x y) (member x y :test #'equal))