List Info

Thread: "ocaml_beginners"::[] Why is this stream empty ?




"ocaml_beginners"::[] Why is this stream empty ?
country flaguser name
United States
2007-06-07 22:55:56

Hello all,

in the code below the stream "stream1" is empty. Why ? I should
expect it to begin with
Genlex.Kwd "type"; Genlex.Kwd"'"; Genlex.Ident"a" (...)

Ewan

let genlexer=Genlex.make_lexer
["+";"-";"*";"/";"=";"(";")";
"let";";in";"if";"then";"else";
"|";"||";";";";;";
"type"];;


let hlexer s=genlexer(Stream.of_string s);;

let stream1=hlexer "type 'a option=None |Some of 'a";;
Stream.count stream1;;

__._,_.___
.

__,_._,___
Re: "ocaml_beginners"::[] Why is this stream empty ?
country flaguser name
United States
2007-06-07 23:08:56

On Fri, 8 Jun 2007, roparzhhemon wrote:

> in the code below the stream "stream1" is empty. Why ? I should
> expect it to begin with
>; Genlex.Kwd "type"; Genlex.Kwd"'"; Genlex.Ident"a" (...)
>
> let genlexer=Genlex.make_lexer
> ["+";"-";"*";"/";"=";"(";")";
> "let";";in";"if";"then";"else";
> "|";"||";";";";;";
>; "type"];;
>
>; let hlexer s=genlexer(Stream.of_string s);;
>;
> let stream1=hlexer "type 'a option=None |Some of 'a";;

The stream is not empty. The first token is Kwd "type", as you expect:

# Stream.next stream1;;
- : Genlex.token = Genlex.Kwd "type"

This does not tell you what you think it does:

> Stream.count stream1;;

A result of 0 means no tokens have been read from the stream yet. Here is
the documentation for Stream.count, from stream.mli:

val count : 'a t -> int
(** Return the current count of the stream elements, i.e. the number
of the stream elements discarded. *)

You can use Stream.empty to test for an empty stream. It has a somewhat
strange behavior in that it returns () if the stream is empty and raises
Stream.Failure if not. You can also just read from the stream, which will
raise Stream.Failure on an empty stream.

Dave

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )