Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Infinite Streams in Caml Light 0.5
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: hedden@e...
Subject: Infinite Streams in Caml Light 0.5
There is a point concerning streams in Caml Light that is not
dealt with in the manual.  This involves the use of function
calls in stream patterns.  Such calls are not evaluated in a lazy
manner, and can cause the system to crash when a recursive
function is used on infinite streams.  For example, the following
function is intended to map a function over all the elements of a

    let rec map_stream func = function
        [< 'x; (map_stream func) strm >] -> [< '(func x); strm >]
      | [< >] -> [< >]

However, as indicated above, if `strm' is infinite, evaluation of 
the recursive call progresses ad infinitum until "out of memory"

A version of the function that does work on infinite streams is:

    let rec map_stream func = function
        [< 'x; strm >] -> [< '(func x); map_stream func strm >]
      | [< >] -> [< >]

In summary, caution is advised when working with function calls 
on infinite streams.  (Of course, we already knew that.  Didn't 
we?  ;-)

Jerry D. Hedden