caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: HEDDEN@ESDSDF.dnet.ge.com
To: caml-list@margaux
Subject: Streams in Caml Light 0.5
Date: Sun, 25 Oct 92 19:33:12 EST	[thread overview]
Message-ID: <9210260033.AA01662@aitgw.ge.com> (raw)

I am having a small difficulty using streams in Caml Light v0.5.

The example below might be a bug, but is more likely due to a
lack of understanding on my part.

The exception in the following toplevel session indicates that
there is a problem:


        >       Caml Light version 0.5

        #include "test";;
        map_stream : ('a -> 'b) -> 'a stream -> 'b stream = <fun>
        filter_stream : ('a -> bool) -> 'a stream -> 'a stream = <fun>
        int_stream : int stream = <abstract>
        - : int = 1
        - : int = 9
        - : int = 25
        odd_sq_stream : int stream = <abstract>
        - : int = 49
        Uncaught exception: Parse_failure
        - : unit = ()
        #quit();;


The file "test.ml" (that is loaded above) contains the following:


        (* Maps a function over a stream *)
        let rec map_stream func = function
            [< 'x >]  ->  [< 'func x >]
          | [< >]     ->  [< >]
        ;;

        (* Filters a stream according to a predicate *)
        let rec filter_stream pred strm =
          match strm with
              [< 'x >]  ->  if pred x then [< 'x >]
                                      else filter_stream pred strm
            | [< >]     ->  [< >]
        ;;

        (* The stream of positive integers *)
        let int_stream = ints 1
          where rec ints n = [< 'n; ints (succ n) >]
        ;;

        (* Use `filter_stream' and `map_stream' to create a
           stream of the squares of the odd integers *)
        (* These work correctly *)
        stream_next (map_stream (function x->x*x)
                     (filter_stream (function n->(n mod 2)= 1)
                     int_stream));;
        stream_next (map_stream (function x->x*x)
                     (filter_stream (function n->(n mod 2)= 1)
                     int_stream));;
        stream_next (map_stream (function x->x*x)
                     (filter_stream (function n->(n mod 2)= 1)
                     int_stream));;

        (* Encapsulate the above *)
        let odd_sq_stream =
                map_stream (function x->x*x)
                (filter_stream (function n->(n mod 2)= 1)
                int_stream)
        ;;

        (* Test the above encapsulation *)
        (* This works correctly *)
        stream_next odd_sq_stream;;

        (* This causes an error *)
        stream_next odd_sq_stream;;


Why is it that the first time `odd_sq_stream' is invoked it returns
the correct value, but on subsequent calls it causes an error?  Is
this a bug, or am I doing something wrong?

Jerry D. Hedden
hedden@esdsdf.dnet.ge.com





             reply	other threads:[~1992-10-26 15:39 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1992-10-26  0:33 HEDDEN [this message]
1992-10-26 16:10 ` Michel Mauny

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9210260033.AA01662@aitgw.ge.com \
    --to=hedden@esdsdf.dnet.ge.com \
    --cc=caml-list@margaux \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).