login  home  contents  what's new  discussion  bug reports     help  links  subscribe  changes  refresh  edit

delay vs. generate - yield

On Date: 01 Mar 2009 14:37:58 +0100 Martin Rubey wrote:

[Due to] the absence of Generators in SPAD, [here is] a mini-tutorial for delay$Stream, which is a partial replacement.

delay takes a function f:() -> Stream, and evaluates it lazily. Warning: it won't work in the interpreter.

Simplest example I can think of:

)abb package TEST Test
Test(): with
        f: Integer -> Stream Integer
    == add
        f n == delay cons(n, f(n+1))
   Compiling FriCAS source code from file 
      using old system compiler.
   TEST abbreviates package Test 
   initializing NRLIB TEST for Test 
   compiling into NRLIB TEST 
   compiling exported f : Integer -> Stream Integer
Time: 0.02 SEC.
(time taken in buildFunctor: 0)
;;; *** |Test| REDEFINED
;;; *** |Test| REDEFINED Time: 0 SEC.
Cumulative Statistics for Constructor Test Time: 0.02 seconds
finalizing NRLIB TEST Processing Test for Browser database: --->-->Test(constructor): Not documented!!!! --->-->Test((f ((Stream (Integer)) (Integer)))): Not documented!!!! --->-->Test(): Missing Description ; compiling file "/var/aw/var/LatexWiki/TEST.NRLIB/TEST.lsp" (written 04 APR 2022 07:13:24 PM):
; /var/aw/var/LatexWiki/TEST.NRLIB/TEST.fasl written ; compilation finished in 0:00:00.012 ------------------------------------------------------------------------ Test is now explicitly exposed in frame initial Test will be automatically loaded when needed from /var/aw/var/LatexWiki/TEST.NRLIB/TEST

then'f 1' will return '[1,...]?'.

f 1

\label{eq1}\left[ 1, \: 2, \: 3, \: 4, \: 5, \: 6, \: 7, \: 8, \: 9, \:{1
0}, \: \ldots \right](1)
Type: Stream(Integer)

Of course the actual output depends on your setting of )set stream calculate, which is usually 10. "lazy" always happens when "delay" is encountered. I.e., when the number of already computed elements is not large enough, computation continues until the number of computed elements is large enough and then until a "delay" is encountered again.

Thus, the aldor style:

  f() == generate {
        yield a;
        yield b;

could become:

  f() == f1()

  f1() == delay
    cons(a, f2())

  f2() == delay
    [b]::Stream R

  Subject:   Be Bold !!
  ( 15 subscribers )  
Please rate this page: