Contents    Page-10    Prev    Next    Page+10    Index   

DFS: Code to Build Answer

Depth-first Search tries operators one at a time; after applying one operator to produce a new state, it calls itself recursively to see if the goal can be reached from that state.


      apply op             search
state ---------> newstate --- ... ---> Goal
  <- (cons op path) <- path = ( ops ) <- ()

If path is a list of ops to get from newstate to a goal, then (cons op path) will get from state to a goal.


(defn search [state]
  (if (goal? state)
      '()   ; no ops needed to get to goal
      (if (failure? state)
          nil
          (let [newstate (op1 state)]
            (let [path (search newstate)]
              (if path
                  (cons 'op1 path)
                  ... try another op
                  )))) ))