$SPAD/src/input is.input

Timothy Daly December 3, 2016

Abstract

Contents

2

1 License

— license —

--Copyright The Numerical Algorithms Group Limited 1991.

———-

— * —

)set break resume
)spool is.output
)set message test on
)set message auto off
)clear all

———-

This is the 3n + 1 function:   f(n) = n/2    if n is even,
                                      3n + 1 otherwise
It can obviously be coded as
 f n ==
   even? n => n quo 2
   3 * n + 1
but we code using the pattern matching syntax:
 f n ==
   n is 2*m% => m%
   3 * n + 1
The 'integer' is to return an integer instead of a polynomial

— * —

--S 1 of 5
f: INT -> INT
--R                                                                   Type: Void
--E 1
--S 2 of 5
f n ==
   not empty?(u := Is(n, 2*m%)) => integer eval(m%, u)
   3 * n + 1
--R
--R                                                                   Type: Void
--E 2
)set stream showall on

———-

The function g generates the stream of the repeated applications of f — * —

--S 3 of 5
g(n:INT):STREAM(INT) == generate(f, n)
--R
--R   Function declaration g : Integer -> Stream(Integer) has been added
--R      to workspace.
--R                                                                   Type: Void
--E 3

———-

There is a conjecture that g(n) gets into the cycle (1 4 2) for every n — * —

--S 4 of 5
s := g 27
--R
--R   Compiling function g with type Integer -> Stream(Integer)
--R   Compiling function f with type Integer -> Integer
--R
--R   (4)  [27,82,41,124,62,31,94,47,142,71,...]
--R                                                        Type: Stream(Integer)
--E 4
--S 5 of 5
extend(s, 150)
--R
--R
--R   (5)
--R   [27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242,
--R    121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350,
--R    175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167,
--R    502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479,
--R    1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734,
--R    1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433,
--R    1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53,
--R    160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20,
--R    10, 5, 16, 8, 4, 2, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4,
--R    2, 7, 22, 11, 34, 17, 52, 26, ...]
--R                                                        Type: Stream(Integer)
--E 5
)spool
)lisp (bye)

———-

References^{[1] nothing}

5