(this is a followup question to this one, which has the missing definitions)
Trying to create a function that can swap two elements in a list of tuples. for example if I had:
[(NW, Just 1),(N, Just 2),(NE, Just 3),(W, Just 4),(M, Just 5),
(E, Just 6),(SW, Just 7),(S, Just 8),(SE, Nothing)]
and I wanted to swap the second elements of S and SE to get
[(NW, Just 1),(N, Just 2),(NE, Just 3),(W, Just 4),(M, Just 5),
(E, Just 6),(SW, Just 7),(S, Nothing),(SE, Just 8)]
How would I go about doing this? I tried creating a function swapper,
swapper :: (Eq a, Eq b) => a -> b -> b -> [(a,b)] -> [(a,b)]
swapper x y z ((a,b):xs) | x == a = ((a,z):xs)
| z == b = ((a,y):xs)
| otherwise = swapper x y z xs
But this returns
[(S,Nothing),(SE,Nothing)]
I can see that I'm not storing the initial tuples or looping after finding one of my goal states. However I'm not sure how to go about doing this in Haskell.
To give a little more context in case required, the four inputs of the function are
(swapper p (label p (Grid b)) (Nothing) b)
where
p = position I want to move to the Nothing space -- (S -> SE)
label p (Grid b) = returns current label of p -- (S = Just 8)
Nothing = passing in Nothing because I was getting errors
when trying to do z == Nothing
b = current board -- ([(NW, Just 1),(N, Just 2),(NE, Just 3),
-- (W, Just 4),(M, Just 5),(E, Just 6),
-- (SW, Just 7),(S, Just 8),(SE, Nothing)])
