Aronson’s Sequence
June 22, 2021
We use the ordinal
function from the previous exercise and the standard front-and-back two-list implementation of queues:
(define (aronson n) ; first n items of aronson's sequence (let loop ((k 1) (n n) (result (list)) (front (string->list "tisthe")) (back (list))) (cond ((zero? n) (reverse result)) ; return result ((null? front) ; rearrange queue, reverse back to front (loop k n result (reverse back) (list))) ((char=? (car front) #\t) ; next item in sequence (loop (+ k 1) (- n 1) (cons k result) (cdr front) (append (filter char-alphabetic? (reverse (string->list (ordinal k)))) back))) (else ; keep searching for next item in sequence (loop (+ k 1) n result (cdr front) back)))))
And here is the output:
> (aronson 100) (1 4 11 16 24 29 33 35 39 45 47 51 56 58 62 64 69 73 78 80 84 89 94 99 104 111 116 122 126 131 136 142 147 158 164 169 174 181 183 193 199 205 208 214 220 226 231 237 243 249 254 270 288 303 307 319 323 341 345 350 362 366 372 383 387 392 407 428 435 450 456 471 477 492 497 514 520 536 542 557 559 578 580 599 604 606 619 621 635 637 651 655 657 667 671 674 676 686 690 695)
You can run the program at https://ideone.com/jySD2l.
In Common Lisp: https://ideone.com/39TR8W