eXept Software AG Logo

Smalltalk/X Webserver

Documentation of class 'LazyCons':

Home

Documentation
www.exept.de
Everywhere
for:
[back]

Class: LazyCons


Inheritance:

   Object
   |
   +--Collection
      |
      +--SequenceableCollection
         |
         +--Cons
            |
            +--LazyCons

Package:
stx:libbasic2
Category:
Collections-Linked
Version:
rev: 1.9 date: 2017/11/28 18:16:01
user: cg
file: LazyCons.st directory: libbasic2
module: stx stc-classLibrary: libbasic2
Author:
Claus Gittinger (Jun 2003)

Description:


This is an experimental (academic ?) goody for demonstration purposes.

A pair with lazy evaluation of the tail.
Useful to implement infinite lists as possible in lazy functional languages.


Related information:



Instance protocol:

accessing-basic
o  cdr
return the tail, second or cdr - whatever you wonna call it.
Here, the tail is evaluated.
This makes me a non-lazy cons.

o  isLazyValue


Examples:


allNumbers represents an infinite list (1..)
  |gen allNumbers|

  gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]].
  allNumbers := gen value:1. 

  allNumbers head.   
  allNumbers tail head. 
  allNumbers tail tail head. 
sieve
  |gen filter sieve primeNumberList|

  gen := [:n | LazyCons car:n cdr:[ gen value:n+1 ]].
  filter := [:n :l |
              |head rest|

              head := l car.
              rest := l cdr.
              (head \\ n) ~~ 0 ifTrue:[
                  LazyCons car:head cdr:[ filter value:n value:rest ].
              ] ifFalse:[
                  filter value:n value:rest.
              ]
            ].

  sieve := [:l |
              |prime rest|

              prime := l car.
              rest := l cdr.
              LazyCons car:prime cdr:[ sieve value:(filter value:prime value:rest) ]
           ].

  primeNumberList := sieve value:(gen value:2).
  primeNumberList


ST/X 7.2.0.0; WebServer 1.670 at bd0aa1f87cdd.unknown:8081; Sat, 20 Apr 2024 19:17:34 GMT