Difference between revisions of "FP Laboratory 7"
Jump to navigation
Jump to search
(Marked this version for translation) |
|||
Line 1: | Line 1: | ||
<translate> | <translate> | ||
− | == List of lists == | + | == List of lists == <!--T:1--> |
</translate> | </translate> | ||
Line 6: | Line 6: | ||
<translate> | <translate> | ||
+ | <!--T:2--> | ||
Consider following type representing picture: | Consider following type representing picture: | ||
</translate> | </translate> | ||
Line 12: | Line 13: | ||
<translate> | <translate> | ||
+ | <!--T:3--> | ||
If you want to print this picture you can use: | If you want to print this picture you can use: | ||
</translate> | </translate> | ||
Line 21: | Line 23: | ||
<translate> | <translate> | ||
+ | <!--T:4--> | ||
Picture example: | Picture example: | ||
</translate> | </translate> | ||
Line 46: | Line 49: | ||
<translate> | <translate> | ||
+ | <!--T:5--> | ||
Create functions that: | Create functions that: | ||
+ | <!--T:6--> | ||
*Flips picture veriticaly and horizontally. | *Flips picture veriticaly and horizontally. | ||
</translate> | </translate> | ||
Line 90: | Line 95: | ||
<translate> | <translate> | ||
+ | <!--T:7--> | ||
*Place one picture above another. | *Place one picture above another. | ||
</translate> | </translate> | ||
Line 122: | Line 128: | ||
<translate> | <translate> | ||
+ | <!--T:8--> | ||
*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). | ||
</translate> | </translate> | ||
Line 154: | Line 161: | ||
<translate> | <translate> | ||
+ | <!--T:9--> | ||
*Rotate picture to the left and to the right. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/l9qIHTFsEV0]] | *Rotate picture to the left and to the right. <div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/l9qIHTFsEV0]] | ||
</translate> | </translate> | ||
Line 210: | Line 218: | ||
<translate> | <translate> | ||
+ | <!--T:10--> | ||
*Increase every point in the picture n times. | *Increase every point in the picture n times. | ||
</translate> | </translate> |
Revision as of 12:26, 20 October 2021
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 = [ "....#....",
"...###...",
"..#.#.#..",
".#..#..#.",
"....#....",
"....#....",
"....#####"]
*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
- Place one picture above another.
above :: Pic -> Pic -> Pic
*Main> pp(above pic pic)
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####
....#....
...###...
..#.#.#..
.#..#..#.
....#....
....#....
....#####
- 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 (++)
- Rotate picture to the left and to the right.
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)
- Increase every point in the picture n times.
zoom :: Int -> Pic -> Pic
*Main> pp(zoom 2 pic)
........##........
........##........
......######......
......######......
....##..##..##....
....##..##..##....
..##....##....##..
..##....##....##..
........##........
........##........
........##........
........##........
........##########
........##########