`*-----------------------------------*`

`*           >>>Pico 2.0<<<          *`

`*         Wolfgang De Meuter        *`

`*  VUB Programming Technology Lab   *`

`*               ©2004               *`

`*-----------------------------------*`

`*       Streams Abstractions        *`

`*-----------------------------------*`

{ stream(hd,tl())::[hd,tl];  `make-stream

  hd(str)::str[1];           `head

  tl(str)::str[2]();         `tail

  map(str(),f(el))::

    stream(f(hd(str())),map(tl(str()),f(el)));

  filter(str,p(el))::

    if(p(hd(str)),

      stream(hd(str),filter(tl(str),p(el))),

      filter(tl(str),p(el)));

  first(n,str)::

    { put(i,str):

      if(i=0,

         t[n]:void,

         { put(i-1,tl(str))[i]:=hd(str)});

      if(n=0,[],put(n,str))};

  zip(str1(),str2())::

    stream([hd(str1()),hd(str2())],zip(tl(str1()),tl(str2())));

  interleave(str1,str2)::

    stream(hd(str1),interleave(str2,tl(str1)));

  prstream(str,n)::{

    rec(str,i):if(i>n,

                  display("..."),

                  { display(",");

                    display(hd(str));

                    rec(tl(str),i+1)});

    display("[");

    display(hd(str));

    rec(tl(str),2) };

  `* SAMPLE CODE *`

  integers(n)::stream(n,integers(n+1));

  Z::integers(1);

  squares::map(Z,el*el);

  even::filter(Z,el\\2=0);

  odd::filter(Z,el\\2=1);

  fibs:void; `for recursion

  fibs:=stream(1,stream(1,map(zip(fibs,tl(fibs)),el[1]+el[2])))

}