LISP Rollback

Can someone help me or explain how to do rollback in LISP? Any examples or references will be appreciated. I tried google, but none of them had a simple example sufficient for understanding.

thanks

+4
source share
1 answer

A typical way is for the non-variable state to be passed along the call stack, and the auxiliary functions will take the current state, returning a new state for the “fake” mutation.

Perhaps (albeit quite suboptimal) Sudoku Solver:

;;; Use a list of 81 integers to represent a sudoku board, ;;; each number 1-9 represents itself, 0 represents a blank (defun sudoku-solver (board) (cond ((notany #'zerop board) (if (sudoku-solved-p board) board nil)) (t (let ((positions (sudoku-all-blanks board))) (loop for position in positions do (loop for number in '(1 2 3 4 5 6 7 8 9) do (let ((result (sudoku-solver (sudoku-set board position number)))) (when result (return-from sudoku-solver result))))))))) 

This will automatically back off until a solution is found. I skipped hiding the demo using the support code, which will turn it from the demo into the actual working code.

+5
source

All Articles