Difference between revisions of "FP Laboratory 7"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 30: Line 30:
 
flipV :: Pic -> Pic
 
flipV :: Pic -> Pic
 
flipH :: Pic -> Pic</syntaxhighlight>
 
flipH :: Pic -> Pic</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
flipV :: Pic -> Pic
 +
flipV = map reverse
 +
 +
flipV' :: Pic -> Pic
 +
flipV' xs = [reverse x|x<-xs]
 +
 +
flipH :: Pic -> Pic
 +
flipH = reverse
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
*Place one picture above another.
 
*Place one picture above another.
 
<syntaxhighlight lang="Haskell">above :: Pic -> Pic -> Pic</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">above :: Pic -> Pic -> Pic</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
above :: Pic -> Pic -> Pic
 +
above x y = x ++ y
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
*Place two pictures side by side (consider, that they have the same height).
 
*Place two pictures side by side (consider, that they have the same height).
 
<syntaxhighlight lang="Haskell">sideBySide :: Pic -> Pic -> Pic</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">sideBySide :: Pic -> Pic -> Pic</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
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 (++)
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
*Rotate picture to the left and to the right.
 
*Rotate picture to the left and to the right.
 
<syntaxhighlight lang="Haskell">
 
<syntaxhighlight lang="Haskell">
 
rotateR :: Pic -> Pic
 
rotateR :: Pic -> Pic
rotateL :: Pic -> Pic</syntaxhighlight>
+
rotateL :: Pic -> Pic
 +
</syntaxhighlight>
 +
 
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
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)
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>
 +
 
 
*Increase every point in the picture n times.
 
*Increase every point in the picture n times.
 
<syntaxhighlight lang="Haskell">zoom :: Int -> Pic -> Pic</syntaxhighlight>
 
<syntaxhighlight lang="Haskell">zoom :: Int -> Pic -> Pic</syntaxhighlight>
 +
 +
<div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution">
 +
<syntaxhighlight lang="Haskell">
 +
zoom :: Int -> Pic -> Pic
 +
zoom n xs = [concat(map (replicate n) x)|x<-concat (map (replicate n) xs)]
 +
</syntaxhighlight>
 +
</div>
 +
<div style="clear:both"></div>

Revision as of 09:49, 24 September 2020

List of lists

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 = [ "....#....",
        "...###...",
        "..#.#.#..",
        ".#..#..#.",
        "....#....",
        "....#....",
        "....#####"]

Create functions that:

  • Flips picture veriticaly and horizontaly.
flipV :: Pic -> Pic
flipH :: Pic -> Pic
flipV :: Pic -> Pic
flipV = map reverse 

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

flipH :: Pic -> Pic
flipH = reverse
  • Place one picture above another.
above :: Pic -> Pic -> Pic
above :: Pic -> Pic -> Pic
above x y = x ++ y
  • Place two pictures side by side (consider, that they have the same height).
sideBySide :: Pic -> 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 (++)
  • Rotate picture to the left and to the right.
rotateR :: Pic -> Pic
rotateL :: Pic -> 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)
  • Increase every point in the picture n times.
zoom :: Int -> Pic -> Pic
zoom :: Int -> Pic -> Pic
zoom n xs = [concat(map (replicate n) x)|x<-concat (map (replicate n) xs)]