# Prolog Streams

Source: prost.pl

Here, "stream" is used in the sense of "sequence", "delayed list", "lazy list" etc. as in Structure and Interpretation of Computer Programs, not in the sense of a Prolog input/output stream.

The file contains the following examples of infinite streams (sequences):
• Stream of 1s
```        ?- ones(Os), seq_take(7, Os, Ts).

Ts = [1, 1, 1, 1, 1, 1, 1]
```
• List patterns (as in Haskell: [1,3..])
```        ?- lp(-5, 2, Ls), seq_take(10, Ls, Ts).

Ts = [-5, 2, 9, 16, 23, 30, 37, 44, 51, 58]
```
• Sequence of primes
```        ?- primes(Ps), seq_take(15, Ps, Ts).

Ts = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
```
• Fibonacci sequence
```        ?- fibs(Fs), seq_take(12, Fs, Ts).

Ts = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
```
• Harmonic sequence (using rational arithmetic)
```        ?- harms(Hs), seq_take(6, Hs, Ts).

Ts = [1, 1 rdiv 2, 1 rdiv 3, 1 rdiv 4, 1 rdiv 5, 1 rdiv 6]
```
In all cases, you can take as many elements as you like.

A program making use of the sequence of primes is meertens.pl. It finds Meertens numbers, i.e., natural numbers N such that N is its own Gödel number (see source for details).
```      ?- time(meertens(M)).
% 1,307,951,016 inferences, 1509.23 CPU in 1538.53 seconds (98% CPU, 866635 Lips)

M = 81312000
```

Main page