## 4 Data Stream Management System in Haskell
### Streams Module  
Table 2 illustrates the operators provided by Haskell to manipulate infinite streams. The basic operators allow to construct infinite streams or to extract elements from the input stream. The high order functions _map_, _inter_, _scan_, and _transp_ allow to apply different transformations on the input streams. For instance, the _map_ function converts each element within a stream, according to a given conversion function. The conversion takes place when the converted values are required by other function. Based on a given operation, the _scan_ function aggregates the elements within a stream into a single value. For computing at each step the sum of a stream of transactional data, the following expression can be used:  
\[scan\ +\ 0\ [2,4,5,3,...]\]  
It provides as output the infinite stream \([0,2,6,11,14,...]\), where the current value sums all the previously ones. The aggregation functions combine elements from two input streams in order to generate a continuous output stream. Consider one wants to add the corresponding values from two financial data streams \(s_{1}\) and \(s_{2}\), expressed by two different currencies:  
\[zipWith\ +\ s_{1}\ (map\ conversion\ s_{2})\]  
where the conversion function is applied on each element from \(s_{2}\). The aggregation of two streams takes place according to an aggregation policy, depending on the time or the configuration of the new tuples. The policy is a function provided as the first input argument for the high order function _zipWith_, which has three input parameters and one output:  
\[zipWith\ policy\ inStream1\ inStream2\ outStream\] \begin{table}
\begin{tabular}{l c c} \hline \hline
**Type** & **Function** & **Signature** \\ \hline Basic & constructor & \(<\):\(>\):a\(-\)\(>\) S a -\(>\) Sa \\  & extract the first element & head:: S a -\(>\) a \\  & extracts the sequence following the stream’s head & tail:: S a -\(>\) S a \\  & takes a stream and returns all its finite prefixes & inits :: S a -\(>\) S ([a]) \\  & takes a stream and returns all its suffixes & tails :: S a -\(>\) S (S a) \\ \hline Transformation & applies a function over all elements & map :: (a -\(>\) b) -\(>\) S a -\(>\) S b \\  & interleaves 2 streams & inter :: Stream a -\(>\) Stream a -\(>\) S a \\  & yields a stream of successive reduced values & scan :: (a -\(>\) b -\(>\) a) -\(>\) S b -\(>\) S a \\  & computes the transposition of a stream of streams & transp :: S (S a) -\(>\) S (S a) \\ \hline Building & repeated applications of a function & iterate :: (a -\(>\) a) -\(>\) a -\(>\) S a \\ streams & constant streams & repeat :: (a -\(>\) S a \\  & returns the infinite repetition of a set of values & cycle :: [a] -\(>\) S a \\ \hline Extracting & takes the first elements & take :: Int -\(>\) S a -\(>\) [a] \\ sublists & drops the first elements & drop :: Int -\(>\) S a -\(>\) S a \\  & returns the longest prefix for which p holds & takeWhile :: (a -\(>\) Bool) -\(>\) S a -\(>\) [a] \\  & returns the suffix remaining after takeWhile & dropWhile :: (a -\(>\) Bool) -\(>\) S a -\(>\) S a \\  & removes elements that do not satisfy p & filter :: a -\(>\) Bool) -\(>\) S a -\(>\) S a \\ \hline Index & returns the element of the stream at index n &!! :: S a -\(>\) Int -\(>\) a \\  & returns the index of the first element equal with \(a\) & elemIndex :: Eq a -\(>\) A -\(>\) S a -\(>\) Int \\  & returns the index of the first element satisfying p & findIndex :: (a -\(>\) Bool) -\(>\) S a -\(>\) Int \\ \hline Aggregation & returns a list of corresponding pairs from 2 streams & zip :: S a -\(>\) S b -\(>\) S (a,b) \\  & combines two streams based on a given function & ZipWith :: (a -\(>\) b -\(>\) c) -\(>\) S a -\(>\) S b -\(>\) S c \\ \hline \hline \end{tabular}
\end{table} 
Table 2: Stream operators in Haskell (\(S\) stands for the \(Stream\) datatype). The elements of the input streams are combined according to the _policy_.