$SPAD/src/input streams.input

Timothy Daly December 3, 2016

Abstract

Contents

2

1 License

— license —

--Copyright The Numerical Algorithms Group Limited 1991.

———-

— * —

)set break resume
)spool streams.output
)set message test on
)set message auto off
)clear all
)set streams calculate 5
)set streams showall on

———-

At least 5 stream elements will be evaluated. The stream of integers starting at 1. — * —

--S 1 of 26
a := [i for i in 1..]
--R
--R
--R   (1)  [1,2,3,4,5,...]
--R                                                Type: Stream(PositiveInteger)
--E 1
--S 2 of 26
b := [i+1 for i in a]
--R
--R
--R   (2)  [2,3,4,5,6,...]
--R                                                Type: Stream(PositiveInteger)
--E 2

———-

Select the 20th element — * —

--S 3 of 26
b.20
--R
--R

--R   (3)  21
--R                                                        Type: PositiveInteger
--E 3

———-

The first 20 elements of b (and a) are evaluated — * —

--S 4 of 26
b
--R
--R
--R   (4)  [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,...]
--R                                                Type: Stream(PositiveInteger)
--E 4
--S 5 of 26
a
--R
--R
--R   (5)  [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,...]
--R                                                Type: Stream(PositiveInteger)
--E 5

———-

The first 10 elements of a — * —

--S 6 of 26
first(a,10)
--R
--R
--R   (6)  [1,2,3,4,5,...]
--R                                                Type: Stream(PositiveInteger)
--E 6

———-

All except the first 10 elements of a — * —

--S 7 of 26
rest(a,10)
--R
--R
--R   (7)  [11,12,13,14,15,16,17,18,19,20,...]
--R                                                Type: Stream(PositiveInteger)
--E 7

———-

The stream of odd integers — * —

--S 8 of 26
[i for i in a | odd? i]
--R
--R
--R   (8)  [1,3,5,7,9,...]
--R                                                Type: Stream(PositiveInteger)
--E 8

———-

Combining two streams — * —

--S 9 of 26
c := [[i,j] for i in a for j in b]
--R
--R
--R   (9)  [[1,2],[2,3],[3,4],[4,5],[5,6],...]
--R                                          Type: Stream(List(PositiveInteger))
--E 9

———-

Selecting the first from each pair — * —

--S 10 of 26
[first i for i in c]
--R
--R
--R   (10)  [1,2,3,4,5,...]
--R                                                Type: Stream(PositiveInteger)
--E 10
)set streams calculate 10

———-

concat(a,b) concatenates streams a and b, better if a is finite — * —

--S 11 of 26
concat([i for i in a while i<7],a)
--R
--R
--R   (11)  [1,2,3,4,5,6,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,...]
--R                                                Type: Stream(PositiveInteger)

--E 11
--S 12 of 26
concat(a,a)
--R
--R
--R   (12)  [1,2,3,4,5,6,7,8,9,10,...]
--R                                                Type: Stream(PositiveInteger)
--E 12
--S 13 of 26
upto:NNI->STREAM INT
--R
--R                                                                   Type: Void
--E 13
--S 14 of 26
upto n == first(a,n)
--R
--R                                                                   Type: Void
--E 14
--S 15 of 26
d := [upto n for n in a]
--R
--R   Compiling function upto with type NonNegativeInteger -> Stream(
--R      Integer)
--R
--R   (15)
--R   [[1], [1,2], [1,2,3], [1,2,3,4], [1,2,3,4,5], [1,2,3,4,5,6],
--R    [1,2,3,4,5,6,7], [1,2,3,4,5,6,7,8], [1,2,3,4,5,6,7,8,9],
--R    [1,2,3,4,5,6,7,8,9,10,...], ...]
--R                                                Type: Stream(Stream(Integer))
--E 15

———-

concat flattens a stream of streams into a one level stream — * —

--S 16 of 26
concat d
--R
--R
--R   (16)  [1,1,2,1,2,3,1,2,3,4,...]
--R                                                        Type: Stream(Integer)
--E 16

———- The sum of a finite stream

— * —

--S 17 of 26
reduce(0,_+$INT,first(a,10))
--R
--R
--R   (17)  55
--R                                                        Type: PositiveInteger
--E 17

———-

A stream of cumulative sums — * —

--S 18 of 26
scan(0,_+$INT,a)
--R
--R
--R   (18)  [1,3,6,10,15,21,28,36,45,55,...]
--R                                                        Type: Stream(Integer)
--E 18
--S 19 of 26
scan(0,_+$INT,[2*i-1 for i in a])
--R
--R
--R   (19)  [1,4,9,16,25,36,49,64,81,100,...]
--R                                                        Type: Stream(Integer)
--E 19
--S 20 of 26
ff:(LIST INT)->(LIST INT)
--R
--R                                                                   Type: Void
--E 20
--S 21 of 26
ff(x)==[x.1+x.2,x.1]
--R
--R                                                                   Type: Void
--E 21

———-

generate(f,x) operates on an A-¿A function f and an initial A, x to produce the stream x,f x,f(f x),...

— * —

--S 22 of 26

fibs := generate(ff,[1,1])
--R
--R   Compiling function ff with type List(Integer) -> List(Integer)
--R
--R   (22)
--R   [[1,1],[2,1],[3,2],[5,3],[8,5],[13,8],[21,13],[34,21],[55,34],[89,55],...]
--R                                           Type: InfiniteTuple(List(Integer))
--E 22
--first([first i for i in fibs], 100)
--S 23 of 26
mt:SQMATRIX(2,INT) := matrix [[1,2],[3,4]]
--R
--R
--R         +1  2+
--R   (23)  |    |
--R         +3  4+
--R                                                Type: SquareMatrix(2,Integer)
--E 23
--S 24 of 26
mplm:SQMATRIX(2,INT)->SQMATRIX(2,INT)
--R
--R                                                                   Type: Void
--E 24
--S 25 of 26
mplm x == x*mt
--R
--R                                                                   Type: Void
--E 25
--S 26 of 26
generate(mplm,mt)
--R
--R   Compiling function mplm with type SquareMatrix(2,Integer) ->
--R      SquareMatrix(2,Integer)
--R
--R   (26)
--R    +1  2+  +7   10+  +37  54 +  +199  290+  +1069  1558+  +5743   8370 +
--R   [|    |, |      |, |       |, |        |, |          |, |            |,
--R    +3  4+  +15  22+  +81  118+  +435  634+  +2337  3406+  +12555  18298+
--R    +30853  44966+  +165751  241570+  +890461   1297782+  +4783807   6972050 +
--R    |            |, |              |, |                |, |                  |,
--R    +67449  98302+  +362355  528106+  +1946673  2837134+  +10458075  15241882+
--R    ...]
--R                                 Type: InfiniteTuple(SquareMatrix(2,Integer))
--E 26
)spool

)lisp (bye) ———-

9

References^{[1] nothing}

10