Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

recursive lisp replace an element maze solver

after defining the matrix :

(setq matriz '((1 0 0 0 0 0)
               (1 1 0 0 0 0)
               (0 1 1 1 0 0)
               (0 0 0 1 0 0)
               (0 0 0 1 1 0)
               (0 0 0 0 1 1)))

I already made a function to get a number according to position (line and column) But now i want to do a function to replace a number in the matrix according to the position and i am having trouble doing it. Lets say i want to replace the position (3 3) that corresponds to the 1 in (0 0 0 1 0 0) i just don't know how to do it. Can only use recursive functions what means no cycles . This I'm working is for maze solver Would appreciate some help thanks :=)

:edited part this is what i have so far

(setq matriz '((1 0 0 0 0 0)(1 1 0 0 0 0)(0 1 1 1 0 0)(0 0 0 1 0 0)(0 0 0 1 1 0)(0 0 0 0 1 1)))


(defun path(i j)
           (list (list (+ i 1) j)
                 (list (- i 1) j)
                 (list i (+ j 1))
                 (list i (- j 1))
                 ))

(defun validsons (lf mat)
           (cond
            ((null lf) nil)
            ((eq (devposmat (caar lf) (cadar lf) mat) 1) (cons (car lf) (validsons (cdr lf) mat)))
            (t (validsons (cdr lf) mat))
           )
          )

(defun Devposicao(i lista)
           (cond
             ((null lista) nil)
             ((< i 0)      nil)
             ((= i 0)      (car lista))
             (t (Devposicao (- i 1) (cdr lista)))))

(defun DevPosMat(i j lista)
(Devposicao j  (Devposicao i lista)))

;shows up avaiable paths (only 1s)

(defun rightpath(i j mat)
           (validsons (path i j) mat)
           )

;so you see the matrix correctly and not a list

(defun writematrix(Mat)
(cond
((null Mat) nil)
(t (progn
(print (car Mat))
(writematrix (cdr Mat))))
)
 )

;this is what i was trying to do to replace

(defun changenumber (i j matriz)
           (cond
            ((null matriz) nil)
            ((< i 0)      nil)
            ((= i 0)      (dec j (car matriz)))
            (t (changenumber (- i 1) j (cdr matriz)))))
(defun dec (pos l)
        (cond
         ((null l) nil)
         ((= pos 0) (cons (- (car l) 1) (cdr l)))
         (t (cons (car l) (dec (- pos 1) (cdr l))))))   

So i am able to use rightpath to move forward and see wich paths i have avaiable, but i just have to edit previous place i was so i dont keep going to my previous position. Sorry if i posted something in the wrong way im not used to this.

like image 889
user2312436 Avatar asked Jun 02 '26 08:06

user2312436


1 Answers

Don't do it...

Common Lisp comes with multidimensional arrays, and it is crazy to use lists for matrices instead.

(defparameter *matrix*
  (make-array '(6 6)
              :element-type 'bit
              :initial-contents
              '((1 0 0 0 0 0)
                (1 1 0 0 0 0)
                (0 1 1 1 0 0)
                (0 0 0 1 0 0)
                (0 0 0 1 1 0)
                (0 0 0 0 1 1))))
(setf (aref *matrix* 3 3) 1)

(see make-array)

...unless forced to

If you are required to use lists by a crazy professor, you can use something like

(setf (car (nthcdr (nth matrix i) j)) 1)

(see nth, nthcdr).

If you are forbidden from using those functions and are required to write your own recursive setter, please state so clearly and show your work (since we are now in the realm of crazy limitations, please also specify if your matrix is supposed to be immutable).

like image 141
sds Avatar answered Jun 06 '26 05:06

sds



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!