Description
Lab 6. Higher-order functions and ADTs
Outline
- ● Higher-order functions and lists in Haskell recap
- ● User-defined types in Haskell recap
- ● Exercise: N-body simulation
Rendering a tilemap
Exercise 6.1.
See prepared code snippet: https://code.world/haskell#PVe5pLviXrxL2FkIRKQ2SQw Implement function renderGrid that renders a Grid of Pictures.
Try using higher-order functions.
Rendering a tilemap (with ADTs)
Exercise 6.2.
See prepared code snippet: https://code.world/haskell#PuRiYbTd_q_icJ2rAyaP6tw Implement functions renderItem, renderTile.
-- | An item that can be placed on a floor tile.
data
= Key |
— | A tile.
data
= | |
-- ^ A key for some door. -- ^ A coin.
-- ^ A wall tile. -- ^ A floor tile, possibly with some item on it. -- ^ A door (with its index).
Item
DoorId
Coin
Tile
Wall
Floor
(Maybe Item)
Door
DoorId
Rendering a tilemap (with ADTs)
Exercise 6.3.
See prepared code snippet: https://code.world/haskell#PuRiYbTd_q_icJ2rAyaP6tw Implement function renderTileGrid.
-- | An item that can be placed on a floor tile.
data
= Key |
— | A tile.
data
= | |
-- ^ A key for some door. -- ^ A coin.
-- ^ A wall tile. -- ^ A floor tile, possibly with some item on it. -- ^ A door (with its index).
Item
DoorId
Coin
Tile
Wall
Floor
(Maybe Item)
Door
DoorId
Modifying a tilemap (with ADTs)
Exercise 6.4.
See prepared code snippet: https://code.world/haskell#PuRiYbTd_q_icJ2rAyaP6tw Implement function removeItems that removes items from all Floor tiles
in a Grid of Tiles.
Exercise 6.5.
Implement function mapGrid that applies a given function to every element in a Grid.
Exercise 6.6.
See prepared code snippet: https://code.world/haskell#PuRiYbTd_q_icJ2rAyaP6tw Implement function openDoors that opens all Door tiles (i.e. replaces them
with Floor tiles) in a Grid of Tiles.
Playing with a tilemap
Exercise 6.7.
See prepared code snippet: https://code.world/haskell#PuRiYbTd_q_icJ2rAyaP6tw Implement function myTileGrid2 by converting myCharGrid.
Exercise 6.8. (*)
Use activityOf to make the game interactive.
Homework (self-study)
- Install Haskell https://www.haskell.org/downloads/
- Read Learn you a Haskell for Great Good Chapters 2, 4, and 5
http://learnyouahaskell.com/chapters
- Test yourself by implementing a program that renders a Koch snowflake of a given rank in Haskell on Code.World platform (https://code.world/haskell):