Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
pp:l09 [2018/05/09 03:06] sergiu created |
pp:l09 [2020/04/25 19:31] (current) pdmatei |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Fie un graf orientat în care fiecare nod are o culoare. | + | ====== Lazy evaluation ====== |
- | Graful este reprezentat în Prolog printr-o listă ce conține doua elemente [C, E]: | + | |
- | * C: o listă de liste [n, c] cu semnificația că nodul n are culoarea c | + | |
- | * E: o listă de liste [n1, n2] cu semnificația că există o muchie de la nodul n1 la nodul n2 | + | |
- | Exemplu: | + | |
- | G = [ | + | |
- | [ [1,galben], [2,rosu], [3,albastru], [4,verde], [5,rosu], | + | |
- | [6,albastru], [7,rosu], [8,galben], [9,albastru], | + | |
- | [10,mov] ], | + | |
- | [ [1,2],[1,3],[2,6],[3,2],[3,4],[4,5],[5,6],[5,7],[5,8], | + | |
- | [6,4],[6,5],[6,7],[7,8],[8,5],[8,9],[8,10],[9,10] ] ]. | + | |
- | ==== Exerciții ==== | + | 1. Consider the following recurrence scheme described informally below. Use it to build the sequence $math[1, 2, \ldots, n!, ...] |
+ | <code> | ||
+ | 4 4*5 4*5*6 ... | ||
+ | 3 3 3 3 | ||
+ | ---------------------- | ||
+ | 3 3*4 3*4*5 3*4*5*6 ... | ||
+ | </code> | ||
- | - Scrieți un predicat getColors care construiește o listă cu toate culorile nodurilor | + | 2. Define the sequence $math[1, \frac{1}{2}, \ldots, \frac{1}{k!}, \ldots ] |
- | - Scrieți un predicat getInEdges care construiește o listă cu toate elementele e din E de tipul (_, X) pentru un X anume | + | |
- | - Scrieți un predicat getOutEdges care construiește o listă cu toate elementele e din E de tipul (X, _) pentru un X anume | + | 3. Write a function which takes a sequence $math[(a_n)_{n\geq 0}] and computes the sequence $math[(s_n)_{n\geq 0}] where $math[s_k = \sum_{k\geq 0} a_k]. Use a strategy similar to that from exercise 1. |
- | - Scrieți un predicat getUniqueColors (același lucru ca getColors dar fără duplicate) | + | |
- | - Scrieți un predicat getNeighbors ce se folosește de predicatele getInEdges și getOutEdges pentru a construi o listă cu nodurile legate de un nod X | + | 4. Write the stream of approximations of $math[e] [[https://en.wikipedia.org/wiki/E_(mathematical_constant) | details ]]. |
- | - Scrieți un predicat getPathsOfLength3 care construiește toate drumurile de lungime 3 ce pleacă dintr-un nod X. Valoarea cu care va unifica rezultatul va fi o listă [X, _, _] | + | |
+ | 5. Write a function which takes a value $math[d], a sequence of approximations $math[(a_n)_{n\geq 0}] and returns that value $math[a_k] from the sequence which satisfies the condition $math[\mid a_k - a_{k+1}\mid \leq d] | ||
+ | |||
+ | 6. Write a function which takes an $math[f], a value $math[a_0] and computes the sequence $math[a_0, f(a_0), f(f(a_0)), \ldots] | ||
+ | |||
+ | 7. The sequence $math[(a_n)_{n\geq 0}] defined as $math[a_{k+1} = (a_k + \frac{n}{a_k})/2], will converge to $math[\sqrt{n}] as $math[k] approaches infinity. Use it to write a function which approximates $math[\sqrt{n}] within 3 decimals. | ||
+ | |||
+ | 8. The diagram below illustrates the approximation of an integral of a continuous function $math[f] between two points $math[a] and $math[b]. The simplest approximation is the area of the rectangle defined by points $math[a] and $math[b] on the $math[Ox] axis, and points $math[f(a)] and $math[f(b)]. | ||
+ | |||
+ | To determine a better approximation, the interval $math[ [a,b] ] is broken in half and we add up the areas of the rectangles: | ||
+ | - $math[a,m,f(a),f(m)] and | ||
+ | - $math[m,b,f(m),f(b)] | ||
+ | <code> | ||
+ | Oy | ||
+ | ^ | ||
+ | f m|. . . . . . . -------- | ||
+ | | / ' \ | ||
+ | | / ' \ f | ||
+ | f b|. . . . . . /. .'. . . . .------- | ||
+ | | ----- ' ' | ||
+ | f a|. . . / ' ' | ||
+ | | / ' ' | ||
+ | | / ' ' ' | ||
+ | | / ' ' ' | ||
+ | ------------------------------> Ox | ||
+ | a m b | ||
+ | |||
+ | </code> | ||
+ | |||
+ | The process can be repeated by recursively dividing up intervals. Write the a function ''integral'' which computes the sequence of approximations of $math[\int_a^b f(x)]. | ||
+ | |||
+ | <code haskell> | ||
+ | integral :: (Float -> Float) -> Float -> Float -> [Float] | ||
+ | </code> | ||
+ | |||
+ | 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> |