Note that there are some explanatory texts on larger screens.

plurals
  1. POA tricky haskell problem
    text
    copied!<p>I had this task for my Haskell class, but I find it quite difficult. If you could help a bit. You are given a maze </p> <pre><code>maze = ["x xxx", "x x", "x x x", "x x ", "xxxxx"] </code></pre> <p>and you can walk through spaces only . You start from (0,1) and the function have to return a string with directions to escape the maze which are :</p> <pre><code>f - forward r- turn right l - turn left </code></pre> <p>And if you have a choice you always prefer right to forward, and forward to left. For the current example the answer is <code>ffllffrffrfflf</code></p> <p>Thanks in advance</p> <pre><code>data Direction = N | W | S | E deriving (Show,Eq) maze = ["x xxx", "x x", "x x x", "x x ", "xxxxx"] d = 's' pos = (0,1) fpath d pos | fst pos == (length maze - 1) = "" | snd (pos) ==0 || (snd ( pos ) == ((length (maze!!0))-1)) = "" | rightPossible d pos = "r" ++ ( fpath (rightRotate d) pos ) | forwardPossible d pos = "f" ++ ( fpath d (nstep d pos) ) | True = "l" ++ fpath (leftRotate d) pos where nstep :: Direction -&gt; (Int, Int) -&gt; (Int, Int) {-next step-} nstep N (x,y) = (x-1,y) nstep W (x,y) = (x,y-1) nstep S (x,y) = (x+1,y) nstep E (x,y) = (x,y+1) rightPossible :: Direction -&gt; (Int, Int) -&gt; Bool rightPossible N (x,y)= (maze !! x)!! (y+1) == ' ' rightPossible W (x,y)= (maze !! (x-1))!! y == ' ' rightPossible S (x,y)= (maze !! x)!! (y-1) == ' ' rightPossible E (x,y)= (maze !! (x+1))!! y == ' ' rightRotate :: Direction -&gt; Direction rightRotate N = E rightRotate W = N rightRotate S = W rightRotate E = S forwardPossible :: Direction -&gt; (Int, Int) -&gt; Bool forwardPossible N (x,y)= ((maze !! (x-1))!! y) == ' ' forwardPossible W (x,y)= ((maze !! x)!! (y-1)) == ' ' forwardPossible S (x,y)= ((maze !! (x+1))!! y) == ' ' forwardPossible E (x,y)= ((maze !! x)!! (y+1)) == ' ' leftRotate :: Direction -&gt; Direction leftRotate N = W leftRotate W = S leftRotate S = E leftRotate E = N </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload