Difference between revisions of "FP Laboratory 7/cs"

From Marek Běhálek Wiki
Jump to navigation Jump to search
(Created page with "nbn")
Line 224: Line 224:
 
<div style="clear:both"></div>
 
<div style="clear:both"></div>
  
== Additional exercises ==
+
nbn
We will use following additional picture:
 
  
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">

Revision as of 11:18, 26 October 2023

Seznamy seznamů

Video logo.png

Uvažujte následující typ reprezentující obrázek:

type Pic = [String]

Pro tisk obrázku využijte následující funkci:

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

Příklad obrázku:

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

Implementujte následující funkce:

  • Funkce, která otočí obrázek vertikáně a horizontálně
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!
  • Funkce, která na sebe položi dva obrázky.
above :: Pic -> Pic -> Pic
*Main> pp(above pic pic)
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####
above :: Pic -> Pic -> Pic
above x y = x ++ y
Try it!
  • Funkce, která položí vedle sebe dva obrázky (uvažujte že obrázky mají stejnou výšku).
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!
  • Funkce, která otočí obrázek doprava a doleva.
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!
  • Funkce, která každý bod v obrázku zvětší n-krát.
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!

nbn

pic2::Pic
pic2 = [ "#########",
         "#.......#",
         "#.......#",
         "#.......#",
         "#.......#",
         "#.......#",
         "#########"]
  • Create a function that performs the superimposition of two images.
superimpose :: Pic -> Pic -> Pic
*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 that for a given integer number n creates a chessboard with dimensions n x n.
chessBoard :: Int -> Pic
*Main> pp(chessBoard 5)
#.#.#
.#.#.
#.#.#
.#.#.
#.#.#
*Main> pp(chessBoard 10)
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
.#.#.#.#.#
#.#.#.#.#.
  • Define a function makePicture where the list arguments gives the positions of the black points (represented by sharp) and the two integer arguments give the width and height of the picture.
makePicture :: Int -> Int -> [(Int,Int)]-> Pic
*Main> pp(makePicture 7 5 [(1,3),(3,2)])
.......
...#...
.......
..#....
.......