1
;The Scheme code in this file provides some compatibility with scripts that
2
;were originally written for use with the older SIOD based Script-Fu plug-in
5
;All items defined in this file except for the random number routines are
6
;deprecated. Existing scripts should be updated to avoid the use of the
7
;compability functions and define statements which follow the random number
10
;The items marked as deprecated at the end of this file may be removed
14
;The random number generator routines below have been slightly reformatted.
15
;A couple of define blocks which are not needed have been commented out.
16
;The original file was called rand2.scm and can be found in:
17
;http://www-2.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/scheme/code/math/random/
19
; Minimal Standard Random Number Generator
20
; Park & Miller, CACM 31(10), Oct 1988, 32 bit integer version.
21
; better constants, as proposed by Park.
39
(hi (quotient *seed* Q))
40
(lo (modulo *seed* Q))
41
(test (- (* A lo) (* R hi)))
45
(set! *seed* (+ test M))
54
; cards 0-9 inclusive (random 10)
58
; Poker Hand Example Probability Calculated
59
; 5 of a kind (aaaaa) 0.0001 0
60
; 4 of a kind (aaaab) 0.0045 0.0053
61
; Full house (aaabb) 0.009 0.0093
62
; 3 of a kind (aaabc) 0.072 0.0682
63
; two pairs (aabbc) 0.108 0.1104
64
; Pair (aabcd) 0.504 0.501
65
; Bust (abcde) 0.3024 0.3058
69
(n (inexact->exact (truncate n)))
73
(let loop ((r (msrg-rand)))
83
; (display "implementation ")
90
; (if (= *seed* 399268537)
91
; (display "looks correct.")
95
; (display " current seed ") (display *seed*)
97
; (display " correct seed 399268537")
104
;This macro defines a while loop which is needed by some older scripts.
105
;This is here since it is not defined in R5RS and could be handy to have.
107
;This while macro was found at:
108
;http://www.aracnet.com/~briand/scheme_eval.html
109
(define-macro (while test . body)
121
;The following define block(s) require the tsx extension to be loaded
128
;Items below this line are for compatability with Script-Fu but
129
;may be useful enough to keep around
131
(define (delq item lis)
138
(while (not (null? lis))
139
(if (not (= item (car lis)))
140
(set! l (append l (list (car lis))))
151
(define (make-list count fill)
152
(vector->list (make-vector count fill))
155
(define (strbreakup str sep)
157
(seplen (string-length sep))
159
(end (string-length str))
167
(while (<= i (- end seplen))
168
(if (substring-equal? sep str i (+ i seplen))
171
(set! l (list (substring str start i)))
172
(set! l (append l (list (substring str start i))))
174
(set! start (+ i seplen))
175
(set! i (+ i seplen -1))
182
(set! l (append l (list (substring str start end))))
190
(define (substring-equal? str str2 start end)
191
(string=? str (substring str2 start end))
194
(define (string-trim str)
195
(string-trim-right (string-trim-left str))
198
(define (string-trim-left str)
200
(strlen (string-length str))
204
(while (and (< i strlen)
205
(char-whitespace? (string-ref str i))
210
(substring str i (string-length str))
214
(define (string-trim-right str)
215
(let ((i (- (string-length str) 1)))
218
(char-whitespace? (string-ref str i))
223
(substring str 0 (+ i 1))
227
(define (unbreakupstr stringlist sep)
228
(let ((str (car stringlist)))
230
(set! stringlist (cdr stringlist))
231
(while (not (null? stringlist))
232
(set! str (string-append str sep (car stringlist)))
233
(set! stringlist (cdr stringlist))
241
;Items below this line are deprecated and should not be used in new scripts.
243
(define aset vector-set!)
244
(define aref vector-ref)
245
(define fopen open-input-file)
248
(define nreverse reverse)
252
(define (print obj . port)
253
(apply write obj port)
257
(define strcat string-append)
258
(define string-lessp string<?)
259
(define symbol-bound? defined?)
260
(define the-environment current-environment)
269
(reverse (cdr (reverse x)))
273
(define (cons-array count type)
275
((long) (make-vector count 0))
276
((short) (make-vector count 0))
277
((byte) (make-vector count 0))
278
((double) (make-vector count 0.0))
279
((string) (make-vector count ""))
285
(- a (* (truncate (/ a b)) b))
288
(define (fread arg1 file)
290
(define (fread-get-chars count file)
297
(set! count (- count 1))
298
(set! c (read-char file))
301
(set! str (string-append str (make-string 1 c)))
314
(set! arg1 (inexact->exact (truncate arg1)))
315
(fread-get-chars arg1 file)
318
(set! arg1 (fread-get-chars (string-length arg1) file))
325
(cons (car (reverse x)) '())
332
(define (prog1 form1 . form2)
335
(if (not (null? form2))
342
(define (rand . modulus)
345
(apply random modulus)
349
(define (strcmp str1 str2)
350
(if (string<? str1 str2)
352
(if (string>? str1 str2)
360
(inexact->exact (truncate n))
365
(if (or (null? n) (not (number? (car n))))