Difference between revisions of "FP Homework 2"

From Marek Běhálek Wiki
Jump to navigation Jump to search
(Created page with "== 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 def...")
 
Line 27: Line 27:
  
 
<syntaxhighlight lang="Haskell" class="myDark" >
 
<syntaxhighlight lang="Haskell" class="myDark" >
Prelude> pp(view (40,15) [Circle (Point 8 4) 5, Box {topLeft = (Point 15 5), bottomRight = (Point 35 12) },Circle (Point 30 12) 8] )
+
Prelude> pp(view (40,15) [Circle (Point 8 4) 5, Box {topLeft = (Point 15 5), bottomRight = (Point 35 12) }, Circle (Point 30 12) 8] )
 
....###...###...........................
 
....###...###...........................
 
....#.......#...........................
 
....#.......#...........................

Revision as of 12:54, 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.

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 - Ticktacktoe