Difference between revisions of "FP Test3 2024"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 170: Line 170:
 
</translate>
 
</translate>
  
<syntaxhighlight lang="Haskell"></syntaxhighlight>
+
<syntaxhighlight lang="Haskell">putIntoMaze :: Maze -> [(Int, Int, Char)] -> Maze</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
 +
ghci> printMaze(putIntoMaze sample2 [(0,0,'1'),(6,6,'2'),(0,6,'3')])
 +
1    3
 +
 +
  ***
 +
  ***
 +
  ***
 +
 +
      2
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 178: Line 186:
 
</translate>
 
</translate>
  
<syntaxhighlight lang="Haskell"></syntaxhighlight>
+
<syntaxhighlight lang="Haskell">
 +
getPart :: Maze -> (Int,Int) -> (Int,Int) -> Maze
 +
</syntaxhighlight>
 
<syntaxhighlight lang="Haskell" class="myDark">
 
<syntaxhighlight lang="Haskell" class="myDark">
 +
ghci> printMaze(getPart sample1 (1,1) (7,7))
 +
*  *
 +
* * *
 +
* * *
 +
  *
 +
******
 +
 +
*******
 
</syntaxhighlight>
 
</syntaxhighlight>
  

Revision as of 11:08, 15 November 2024

Home Preparation For Test 3 - 2024

Data definition

Consider following type representing a maze:

type Maze = [String]

If you want to print this maze on a screen you can use:

printMaze :: Maze -> IO ()
printMaze x = putStr (concat (map (++"\n") x))

Maze example:

sample1 :: Maze
sample1 = ["*********",
           "* *   * *",
           "* * * * *",
           "* * * * *",
           "*   *   *",
           "******* *",
           "        *",
           "*********"]

sample2 :: Maze
sample2 = ["       ",
           "       ",
           "  ***  ",
           "  ***  ",
           "  ***  ",
           "       ",
           "       "]


sample3 :: Maze
sample3 = ["  * *  ",
           " ##### ",
           "  ***  ",
           "  * *  ",
           "  ***  ",
           "     * ",
           "       "]

arrow :: Maze
arrow = [ "....#....",
          "...###...",
          "..#.#.#..",
          ".#..#..#.",
          "....#....",
          "....#....",
          "....#####"]
ghci> printMaze sample1                                           
*********
* *   * *
* * * * *
* * * * *
*   *   *
******* *
        *
*********

Tasks

Create functions that:

  • Place one maze above another.
above :: Maze -> Maze -> Maze
*Main> printMaze(above arrow arrow)
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####
  • Place two mazes side by side (consider, that they have the same height).
sideBySide :: Maze -> Maze -> Maze
*Main> printMaze(sideBySide arrow arrow)
....#........#....
...###......###...
..#.#.#....#.#.#..
.#..#..#..#..#..#.
....#........#....
....#........#....
....#####....#####
  • Rotate maze to the left and to the right.
rotateR :: Maze -> Maze
rotateL :: Maze -> Maze
*Main> printMaze (rotateR arrow)       
.......
...#...
....#..
.....#.
#######
#....#.
#...#..
#..#...
#......
*Main> printMaze(rotateL arrow)
......#
...#..#
..#...#
.#....#
#######
.#.....
..#....
...#...
.......
  • Define a function getFromMaze that takes a character from a maze. The coordinates are given as: (row index, column index) where the indexes start from (0,0). The coordinate (0,0) is a top left corner.
getFromMaze :: Maze -> (Int, Int) -> Char
ghci> getFromMaze  sample1  (1,1)
' '
  • Define a function
putIntoMaze :: Maze -> [(Int, Int, Char)] -> Maze
ghci> printMaze(putIntoMaze sample2 [(0,0,'1'),(6,6,'2'),(0,6,'3')]) 
1     3

  ***
  ***
  ***

      2
  • Define a function
getPart :: Maze -> (Int,Int) -> (Int,Int) -> Maze
ghci> printMaze(getPart sample1 (1,1) (7,7))
 *   *
 * * *
 * * *
   *
******

*******
  • Define a function