0

I want to create game Magic 15 Puzzle in Haskell I have function set :: [[Char]] -> Char -> [[Char]] . It switches Char with empty space in [[Char]].

*Main> pp puzzle2
AC DE
FBHIJ
KGLNO
PQMRS
UVWXT
*Main> pp (set puzzle2 'C')
A CDE
FBHIJ
KGLNO
PQMRS
UVWXT
*Main> 

Now I want to do recursion for [Char] (or String) like this (To do set xs for previous set x)

puzzle :: Result -> [Char] -> Result
puzzle gameboard (x:xs) =  set (set (x:xs) x) xs 

But compilation says it is error:

 Couldn't match expected type ‘Char’ with actual type ‘[Char]’

I expect this output:

*Main> pp(puzzle puzzle2 "CB")                    
ABCDE
F HIJ
KGLNO
PQMRS
UVWXT
      

What can I do to solve this? Thanks a lot in advance for answer!

Whole Code:

import Data.Char
type Result = [String]
pp :: Result -> IO ()
pp x = putStr (concat (map (++"\n") x))

puzzle2 :: [[Char]]
puzzle2 = ["AC DE",
           "FBHIJ",
           "KGLNO",
           "PQMRS",
           "UVWXT"]





getCords board x = head ( head [[(row_index, column_index) |(column_index, char) <- zip[1..] row, x == char] |(row_index,row)<- zip [1..]board,x `elem` row])
getRow board c = fst ( getCords board c)
getCol board c = snd ( getCords board c)
check ch1 ch2 board =  (getRow board ch2 == getRow board ch1 + 1 || getRow board ch2 == getRow board ch1 - 1) && (getCol board ch1 == getCol board ch2) || ((getRow board ch1 == getRow board ch2) && (getCol board ch2 == getCol board ch1 + 1 || getCol board ch2 == getCol board ch1 - 1) )





set gameboard x | check x ' ' gameboard = [[if ch == ' ' then x else if ch == x then ' ' else ch | ch<- line] | line<-gameboard]
                | not (check x ' ' gameboard ) = [[ch | ch<- line] | line<-gameboard]





puzzle :: Result -> [Char] -> Result
puzzle gameboard (x:xs) =  set (set (x:xs) x) xs 

1 Answer 1

1

Just change the last function to

puzzle :: Result -> [Char] -> Result
puzzle g [] = g
puzzle g (x:xs) =  puzzle (set g x) xs 
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot, it works, what does puzzle g [] = g means?
it means that for empty string it doesn't changes, yes?
Yes, it is the condition of end of recursion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.