Reputation: 368
I'm trying to implement a function that returns a list of LIST (each list in LIST is the result of two elements swapped in list). It's supposed to do a search based on the list formed from each swap. It is part of my program to solve the 8 puzzle problem. Here's what i have so far
(setq *LIST* nil)
(defun swapped_list(lst)
(loop for j in (positions_to_swap) do
(setq *LIST* (rotatef (nth pos lst) (nth j lst))
*LIST*)
(swapped_list '(11 12 13 14 15 16 17 18 19))
If positions_to_swap
is (0 2 5)
and pos
is 4
, this should return
((15 12 13 14 11 16 17 18 19) (11 12 15 14 13 16 17 18 19) (11 12 13 14 16 15 17 18 19))
I've been spending countless hours trying to debug with no progress. I've tried many variants but none of them work.
Upvotes: 2
Views: 2813
Reputation: 14291
If positions_to_swap is (0 2 5) and pos is 4, this should return ((15 12 13 14 11 16 17 18 19) (11 12 15 14 13 16 17 18 19) (11 12 13 14 16 15 17 18 19))
(defun swap (list position positions-to-swap)
(loop for position-to-swap in positions-to-swap
for rotated-list = (copy-list list)
do (rotatef (nth position rotated-list)
(nth position-to-swap rotated-list))
collect rotated-list))
Does the trick:
CL-USER> (swap '(11 12 13 14 15 16 17 18 19) 4 '(0 2 5))
((15 12 13 14 11 16 17 18 19)
(11 12 15 14 13 16 17 18 19)
(11 12 13 14 16 15 17 18 19))
Upvotes: 1