Difference between revisions of "FP Laboratory 7"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 297: Line 297:
 
####.####
 
####.####
 
####.....
 
####.....
 +
</syntaxhighlight>
 +
 +
<translate>
 +
* Create a function ChessBoard that for given integer number n creates a chessboard.
 +
</translate>
 +
 +
<syntaxhighlight lang="Haskell">chessBoard :: Int -> Pic</syntaxhighlight>
 +
<syntaxhighlight lang="Haskell" class="myDark">
 +
*Main> pp(chessBoard 5)
 +
#.#.#
 +
.#.#.
 +
#.#.#
 +
.#.#.
 +
#.#.#
 +
*Main> pp(chessBoard 10)
 +
.#.#.#.#.#
 +
#.#.#.#.#.
 +
.#.#.#.#.#
 +
#.#.#.#.#.
 +
.#.#.#.#.#
 +
#.#.#.#.#.
 +
.#.#.#.#.#
 +
#.#.#.#.#.
 +
.#.#.#.#.#
 +
#.#.#.#.#.
 
</syntaxhighlight>
 
</syntaxhighlight>

Revision as of 12:42, 4 August 2023

List of lists

Video logo.png

Consider following type representing picture:

type Pic = [String]

If you want to print this picture you can use:

pp :: Pic -> IO ()
pp x = putStr (concat (map (++"\n") x))

Picture example:

pic :: Pic
pic = [ "....#....",
        "...###...",
        "..#.#.#..",
        ".#..#..#.",
        "....#....",
        "....#....",
        "....#####"]
*Main> pp pic
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####

Create functions that:

  • Flips picture veriticaly and horizontally.
flipV :: Pic -> Pic
flipH :: Pic -> Pic
*Main> pp(flipV pic)
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
#####....
*Main> pp(flipH pic)
....#####
....#....
....#....
.#..#..#.
..#.#.#..
...###...
....#....


flipV :: Pic -> Pic
flipV = map reverse 

flipV' :: Pic -> Pic
flipV' xs = [reverse x|x<-xs]

flipH :: Pic -> Pic
flipH = reverse
Try it!
  • Place one picture above another.
above :: Pic -> Pic -> Pic
*Main> pp(above pic pic)
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####
above :: Pic -> Pic -> Pic
above x y = x ++ y
Try it!
  • Place two pictures side by side (consider, that they have the same height).
sideBySide :: Pic -> Pic -> Pic
*Main> pp(sideBySide pic pic)
....#........#....
...###......###...
..#.#.#....#.#.#..
.#..#..#..#..#..#.
....#........#....
....#........#....
....#####....#####
sideBySide :: Pic -> Pic -> Pic
sideBySide xs ys = map (\(x,y) -> x ++ y)(zip xs ys) 

sideBySide':: Pic -> Pic -> Pic
sideBySide' (x:xs) (y:ys) = (x ++ y) : sideBySide' xs ys
sideBySide' _ _ = []

sideBySide'' :: Pic -> Pic -> Pic
sideBySide'' = zipWith (++)
Try it!
  • Rotate picture to the left and to the right.
Video logo.png
rotateR :: Pic -> Pic
rotateL :: Pic -> Pic
*Main> pp(rotateR pic)       
.......
...#...
....#..
.....#.
#######
#....#.
#...#..
#..#...
#......
*Main> pp(rotateL pic)
......#
...#..#
..#...#
.#....#
#######
.#.....
..#....
...#...
.......
toRow :: String -> Pic
toRow xs = map (\x -> [x]) xs -- [[x]|x<-xs]

rotateR :: Pic -> Pic
rotateR [x] = toRow x
rotateR (x:xs) = (rotateR xs) `sideBySide` (toRow x)

rotateR' :: Pic -> Pic
rotateR' x = foldl1 sideBySide (reverse (map toRow x))


rotateL :: Pic -> Pic
rotateL [x] = reverse(toRow x)
rotateL (x:xs) = reverse(toRow x) `sideBySide` (rotateL xs)

rotateL' :: Pic -> Pic
rotateL' x = foldl1 sideBySide (map (reverse.toRow) x)
Try it!
  • Increase every point in the picture n times.
zoom :: Int -> Pic -> Pic
*Main> pp(zoom 2 pic)
........##........
........##........
......######......
......######......
....##..##..##....
....##..##..##....
..##....##....##..
..##....##....##..
........##........
........##........
........##........
........##........
........##########
........##########
zoom :: Int -> Pic -> Pic
zoom n xs = [concat(map (replicate n) x)|x<-concat (map (replicate n) xs)]
Try it!

Additional exercises

  • Create a function that performs the superimposition of two images.
superimpose :: Pic -> Pic -> Pic

Consider the following additional picture:

pic2::Pic
pic2 = [ "#########",
         "#.......#",
         "#.......#",
         "#.......#",
         "#.......#",
         "#.......#",
         "#########"]
*Main> pp(superimpose pic pic2)
#########
#..###..#
#.#.#.#.#
##..#..##
#...#...#
#...#...#
#########
  • Create a function that inverts "colors" in a given picture.
invertColors :: Pic -> Pic
*Main> pp(invertColors pic)
####.####
###...###
##.#.#.##
#.##.##.#
####.####
####.####
####.....
  • Create a function ChessBoard that for given integer number n creates a chessboard.
chessBoard :: Int -> Pic
*Main> pp(chessBoard 5)
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#
*Main> pp(chessBoard 10)
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.