File: strutl.scm

package info (click to toggle)
scmutils 0~20230125%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,028 kB
  • sloc: lisp: 78,935; sh: 32; makefile: 10
file content (179 lines) | stat: -rw-r--r-- 4,319 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#| -*- Scheme -*-

Copyright (c) 1987, 1988, 1989, 1990, 1991, 1995, 1997, 1998,
              1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
              2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014,
              2015, 2016, 2017, 2018, 2019, 2020
            Massachusetts Institute of Technology

This file is part of MIT scmutils.

MIT scmutils is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

MIT scmutils is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with MIT scmutils; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.

|#

;;;; STRUTL.SCM -- Stream utilities

(declare (usual-integrations))

(define stream:for-each
 (let ()
   (define (loop p s n)
     (cond ((empty-stream? s)
	    'done)
	   ((int:= n 1)
	    (p (head s))
	    '...)
	   (else
	    (p (head s))
	    (loop p (tail s) (int:- n 1)))))
   (lambda (p s . optionals)
     (loop p s
	   (if (not (null? optionals))
	       (car optionals)
	       -1)))))


(define print-stream
  (lambda (s . optionals)
     (apply stream:for-each write-line s optionals)))

(define (combiner-padded-streams f pad)
  (define (lp ss)
    (cons-stream (apply f
			(map (lambda (s)
			       (if (null? s)
				   pad
				   (head s)))
			     ss))
		 (lp (map (lambda (s)
			    (if (null? s)
				s
				(tail s)))
			  ss))))
  (lambda args (lp args)))


(define (stream-of-iterates next value)
  (cons-stream value
	       (stream-of-iterates next (next value))))


(define (infinite-stream-of x)
  (cons-stream x
	       (infinite-stream-of x)))


(define (stream-evaluate s x)
  (map-stream (lambda (f) (f x)) s))

(define (stream-apply s x)
  (map-stream (lambda (f) (apply f x)) s))


(define (map-stream f s)
  (if (empty-stream? s)
      the-empty-stream
      (cons-stream (f (head s))
		   (map-stream f (tail s)))))

(define map-streams stream-map)


(define (merge-streams s1 s2)
  (cons-stream (stream-car s1)
	       (cons-stream (stream-car s2)
			    (merge-streams (stream-cdr s1)
					   (stream-cdr s2)))))

(define (shorten-stream n s)
  (if (or (fix:= n 0) (empty-stream? s))
      the-empty-stream
      (cons-stream (head s)
		   (shorten-stream (fix:- n 1)
				   (tail s)))))

(define (stream:+ s1 s2)
  (map-streams g:+ s1 s2))

(define (stream:- s1 s2)
  (map-streams g:- s1 s2))

(define (stream:* s1 s2)
  (map-streams g:* s1 s2))

(define (stream:/ s1 s2)
  (map-streams g:/ s1 s2))



(define zero-stream 
  (cons-stream :zero zero-stream))

(define one-stream
  (cons-stream :one one-stream))

(define (integers-starting-from n)
  (cons-stream n (integers-starting-from (int:+ n 1))))


(define natural-number-stream
  (cons-stream :one
	       (stream:+ one-stream
			 natural-number-stream)))

(define factorial-stream
  (let ()
    (define (fact-helper n! n+1)
      (cons-stream n!
		   (fact-helper (g:* n+1 n!) (g:+ n+1 1))))
    (fact-helper 1 1)))

(define (stream-of-powers x unity)
  (stream-of-iterates (lambda (y) (g:* x y))
		      unity))


(define stream-for-each
  (access stream-for-each (->environment '(runtime stream))))

(define stream-append
  (access stream-append (->environment '(runtime stream))))

(define stream-filter
  (access stream-filter (->environment '(runtime stream))))

(define stream-accumulate
  (access stream-accumulate (->environment '(runtime stream))))

;;; MIT Scheme system provides 
;;;  PRIME-NUMBERS-STREAM

;;; expands a stream with zeros interpolated between given values.

(define (stream:inflate stream n-interpolated-zeros)
  (cons-stream (head stream)
	       (stream:list-append (make-list n-interpolated-zeros
					      (g:zero-like (head stream)))
				   (stream:inflate (tail stream)
						   n-interpolated-zeros))))

(define (stream:list-append list stream)
  (if (null? list)
      stream
      (cons-stream (car list)
		   (stream:list-append (cdr list)
				       stream))))