This is an old revision of the document!
Lazy evaluation
1. Consider the following recurrence scheme described informally below. Use it to build the sequence $ 1, 2, \ldots, n!, \ldots$
4 4*5 4*5*6 ... 3 3 3 3 ---------------------- 3 3*4 3*4*5 3*4*5*6 ...
2. Define the sequence $ 1, \frac{1}{2}, \ldots, \frac{1}{k!}, \ldots $
3. Write a function which takes a sequence $ (a_n)_{n\geq 0}$ and computes the sequence $ (s_n)_{n\geq 0}$ where $ s_k = \sum_{k\geq 0} a_k$ . Use a strategy similar to that from exercise 1.
4. Write the stream of approximations of $ e$ details .
5. Write a function which takes a value $ d$ , a sequence of approximations $ (a_n)_{n\geq 0}$ and returns that value $ a_k$ from the sequence which satisfies the condition $ \mid a_k - a_{k+1}\mid \leq d$
6. Write a function which takes an $ f$ , a value $ a_0$ and computes the sequence $ a_0, f(a_0), f(f(a_0)), \ldots$
7. The sequence $ (a_n)_{n\geq 0}$ defined as $ a_{k+1} = (a_k + \frac{n}{a_k})/2$ , will converge to $ \sqrt{n}$ as $ k$ approaches infinity. Use it to write a function which approximates $ \sqrt{n}$ within 3 decimals.
8. The diagram below illustrates the approximation of an integral of a continuous function $ f$ between two points $ a$ and $ b$ . The simplest approximation is the area of the rectangle defined by points $ a$ and $ b$ on the $ Ox$ axis, and points $ f(a)$ and $ f(b)$ .
To determine a better approximation, the interval $ $ [ [a,b] ] is broken in half and we add up the areas of the rectangles:
- $ a,m,f(a),f(m)$ and
- $ m,b,f(m),f(b)$
Oy ^ f m|. . . . . . . -------- | / ' \ | / ' \ f f b|. . . . . . /. .'. . . . .------- | ----- ' ' f a|. . . / ' ' | / ' ' | / ' ' ' | / ' ' ' ------------------------------> Ox a m b
The process can be repeated by recursively dividing up intervals. Write the a function integral
which computes the sequence of approximations of $ \int_a^b f(x)$ .
integral :: (Float -> Float) -> Float -> Float -> [Float]
9. It is likely that your implementation will recompute (unnecessarily) the values $ 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):
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)
Write the function at
which returns the value of the position x
,y
in the map:
at :: Map -> Int -> Int -> Maybe Char
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 #
).