Difference between revisions of "FP Homework 2"
Line 101: | Line 101: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | Write a function <code>fill</code> that takes a defined picture and a starting position and it fills the continuous area of free cells starting from the defined starting position. | + | Write a function <code>fill</code> that takes a defined picture and a starting position (tuple (column, row)) and it fills the continuous area of free cells starting from the defined starting position with character <code>'*'</code>. |
<syntaxhighlight lang="Haskell"> | <syntaxhighlight lang="Haskell"> | ||
fill :: Result -> (Int,Int) -> Result | fill :: Result -> (Int,Int) -> Result |
Revision as of 15:53, 20 November 2019
Basic notes
In all exercises you are required to write something to standard output. You can use the same strategy as in Laboratory 7.
Lets define a type for the result:
type Result = [String]
Now, if you want to print this result nicely on the screen, you can use:
pp :: Result -> IO ()
pp x = putStr (concat (map (++"\n") x))
1 - Painting
Lets define a new data types representing a circle and a rectangle.
data Point = Point Int Int
data Shape = Circle Point Int
| Rectangle {topLeft:: Point, bottomRight::Point}
Using these types write a function view
that creates a view of defined shapes. The first parameter is a tuple (columns, rows) defining the size of the resulting view. Left top corner has a coordinate (0,0). Second argument is a list of shapes (either circles or rectangles).
view :: (Int,Int) -> [Shape] -> Result
The result may differ based on rounding. In following example '.'
was used for the free spot, '#'
for the filled spot.
Prelude>pp(view (40,15) [Circle (Point 8 4) 5, Box {topLeft = (Point 15 5), bottomRight = (Point 35 12) }, Circle (Point 30 12) 8] )
....###...###...........................
....#.......#...........................
...##.......##..........................
...#.........#..........................
...#.........#.............#######......
...#.........#.#####################....
...##.......##.#........##.........##...
....#.......#..#.......##..........###..
....###...###..#.......#...........#.#..
......#####....#......##...........#.##.
...............#......#............#..#.
...............#......#............#..#.
...............#####################..#.
......................#...............#.
......................#...............#.
2 - Drawing
Lets define a new data types representing a point and a line.
data Point = Point Int Int
data Line = Line Point Point
Using these types write a function drawLines
that creates a view of defined lines. The first parameter is a tuple (columns, rows) defining the size of the resulting view. Left top corner has a coordinate (0,0). Second argument is a list of lines.
drawLines :: (Int,Int) -> [Line] -> Result
The result may differ based on rounding. In following example '.'
was used for the free spot, '#'
for the filled spot.
Prelude>pp(drawLines (31,15) [Line (Point x y) (Point 15 7)|(x,y)<-concat [[(x,y)|y<-[0,7,14]]|x<-[0,15,30]]])
##.............#.............##
..##...........#...........##..
....##.........#.........##....
.....###.......#.......###.....
........##.....#.....##........
..........###..#..###..........
.............#####.............
###############################
.............#####.............
..........###..#..###..........
........##.....#.....##........
.....###.......#.......###.....
....##.........#.........##....
..##...........#...........##..
##.............#.............##
3 - Filling
Lets define a picture that is composed from '.'
which is used for a free spot and '#'
is used for the filled spot.
sampleInput =
["....................",
"....................",
"....#####...........",
"...##...##..........",
"..##.....##.........",
"..#.......#.........",
"..#...############..",
"..#...#...#......#..",
"..##..#..##......#..",
"...##.#.##.......#..",
"....#####........#..",
"......#..........#..",
"......############..",
"....................",
"...................."]
Write a function fill
that takes a defined picture and a starting position (tuple (column, row)) and it fills the continuous area of free cells starting from the defined starting position with character '*'
.
fill :: Result -> (Int,Int) -> Result
Prelude> pp(fill sampleInput (0,0))
********************
********************
****#####***********
***##...##**********
**##.....##*********
**#.......#*********
**#...############**
**#...#...#......#**
**##..#..##......#**
***##.#.##.......#**
****#####........#**
******#..........#**
******############**
********************
********************