Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
pp:l09 [2020/04/25 19:21]
pdmatei
pp:l09 [2020/04/25 19:31] (current)
pdmatei
Line 51: Line 51:
 9. It is likely that your implementation will recompute (unnecessarily) the values $math[f(a), m, f(m), f(b)] in recursive steps. Write an alternative implementation which avoids this. 9. It is likely that your implementation will recompute (unnecessarily) the values $math[f(a), m, f(m), f(b)] in recursive steps. Write an alternative implementation which avoids this.
  
 +10. Consider a representation of maps (with obstacles as follows):
 +<code haskell>
 +l1=" ​  # ​    "​
 +l2=" #   # # "
 +l3=" # ### # "
 +l4=" #     # "
 +l5=" ####### "
 +l6=" ​        "​
  
 +data Map = Map [String]
  
 +instance Show Map where
 +  show (Map m) = "​\n"​ ++ foldr (\x acc->​x++"​\n"​++acc) [] m   
 +
 +m = Map [l1,​l2,​l3,​l4,​l5,​l6]
 +
 +type State = (Int,Int)
 +
 +</​code> ​
 +
 +Write the function ''​at''​ which returns the value of the position ''​x'',''​y''​ in the map:
 +<code haskell>
 +at :: Map -> Int -> Int -> Maybe Char
 +</​code>​
 +
 +11. Define a function which computes, for a given position, the list of valid //next positions// (a valid position is one that is on the map, and it is not a //wall//, i.e. a ''#''​). Hint, use the list ''​[(x-1,​y-1),​(x-1,​y),​(x-1,​y+1),​(x,​y-1),​(x,​y+1),​(x+1,​y-1),​(x+1,​y),​(x+1,​y+1)]''​.
 +
 +12. Implement the type ''​Tree a''​ of trees with arbitrary number of children nodes.
 +
 +13. Enrol ''​Tree''​ in class ''​Functor''​ (see classes), and define the function ''​fmap''​.
 +
 +14. Write a function which takes a (possibly infinite) tree and returns the sub-tree where each branch is of length at most ''​k'':​
 +<code haskell>
 +take_t :: Integer -> Tree a -> Tree a
 +</​code>​
 +
 +15. Implement the infinite tree of valid positions, starting from an initial one. In this tree, paths represent trails exploring the map.
 +<code haskell>
 +make_st_tree :: Map -> State -> Tree State
 +</​code>​
 +16. Implement the function ''​toMap''​ which //draws// a position on the map (using the character ''​.''​).
 +<code haskell>
 +toMap :: State -> Map -> Map
 +</​code>​
 +
 +17. Implement the tree of possible //trails// through the map:
 +<code haskell>
 +make_map_tree :: Map -> State -> Tree Map
 +</​code>​