~ubuntu-branches/ubuntu/trusty/librep/trusty

« back to all changes in this revision

Viewing changes to lisp/unscheme/data.jl

  • Committer: Bazaar Package Importer
  • Author(s): Christian Marillat
  • Date: 2001-11-13 15:06:22 UTC
  • Revision ID: james.westby@ubuntu.com-20011113150622-vgmgmk6srj3kldr3
Tags: upstream-0.15.2
ImportĀ upstreamĀ versionĀ 0.15.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#| data.jl -- data type functions
 
2
 
 
3
   $Id: data.jl,v 1.3 2001/08/08 06:00:23 jsh Exp $
 
4
 
 
5
   Copyright (C) 2000 John Harper <john@dcs.warwick.ac.uk>
 
6
 
 
7
   This file is part of librep.
 
8
 
 
9
   librep is free software; you can redistribute it and/or modify it
 
10
   under the terms of the GNU General Public License as published by
 
11
   the Free Software Foundation; either version 2, or (at your option)
 
12
   any later version.
 
13
 
 
14
   librep is distributed in the hope that it will be useful, but
 
15
   WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
   GNU General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU General Public License
 
20
   along with Jade; see the file COPYING.  If not, write to
 
21
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 
22
|#
 
23
 
 
24
(define-structure unscheme.data
 
25
 
 
26
    (export #f #t not eqv? eq? equal? boolean?
 
27
 
 
28
            pair? cons car cdr set-car! set-cdr!
 
29
            caar cadr cdar cddr
 
30
            caaar caadr cadar caddr
 
31
            cdaar cdadr cddar cdddr
 
32
            caaaar caaadr caadar caaddr
 
33
            cadaar cadadr caddar cadddr
 
34
            cdaaar cdaadr cdadar cdaddr
 
35
            cddaar cddadr cdddar cddddr
 
36
 
 
37
            null? list? list length append reverse
 
38
            list-tail list-ref memq memv member
 
39
            assq assv assoc
 
40
 
 
41
            symbol? symbol->string string->symbol
 
42
            
 
43
            number? complex? real? rational? integer?
 
44
            exact? inexact? = < > <= >= zero? positive?
 
45
            negative? odd? even? max min + * - /
 
46
            abs quotient remainder modulo gcd lcm
 
47
            numerator denominator floor ceiling
 
48
            truncate round rationalize exp log sin cos
 
49
            tan asin acos atan sqrt expt exact->inexact
 
50
            inexact->exact string->number number->string
 
51
            
 
52
            char? char=? char<? char>? char<=? char>=?
 
53
            char-ci=? char-ci<? char-ci>? char-ci<=?
 
54
            char-ci>=? char-alphabetic? char-numeric?
 
55
            char-whitespace? char-upper-case?
 
56
            char-lower-case? char->integer integer->char
 
57
            char-upcase char-downcase
 
58
            
 
59
            string? make-string string string-length
 
60
            string-ref string-set! string=? string-ci=?
 
61
            string<? string>? string<=? string>=?
 
62
            string-ci<? string-ci>? string-ci<=?
 
63
            string-ci>=? substring string-append
 
64
            string->list list->string string-copy
 
65
            string-fill!
 
66
            
 
67
            vector? make-vector vector vector-length
 
68
            vector-ref vector-set! vector->list
 
69
            list->vector vector-fill!)
 
70
 
 
71
    ((open rep)
 
72
     (access rep))
 
73
 
 
74
  (defconst #f ())
 
75
  (defconst #t t)
 
76
 
 
77
;;; equivalence
 
78
 
 
79
  (define eqv? eql)
 
80
  (define eq? eq)
 
81
  (define equal? equal)
 
82
 
 
83
  (define (boolean? obj) (and (memq obj '(() t #f #t)) #t))
 
84
 
 
85
;;; pairs (cons cells)
 
86
 
 
87
  (define pair? consp)
 
88
 
 
89
  (define set-car! rplaca)
 
90
  (define set-cdr! rplacd)
 
91
 
 
92
;;; lists
 
93
 
 
94
  (define null? null)
 
95
 
 
96
  (define (list? x)
 
97
    (let loop ((slow x)
 
98
               (fast (cdr x)))
 
99
      (cond ((null slow) #t)
 
100
            ((not (consp slow)) #f)
 
101
            ((eq slow fast) #f)
 
102
            (t (loop (cdr slow) (cddr fast))))))
 
103
 
 
104
  ;; XXX return nil if I > (length LST)
 
105
  (define (list-tail lst i) (nthcdr i lst))
 
106
  (define (list-ref lst i) (nth i lst))
 
107
 
 
108
  (define memv memql)
 
109
 
 
110
  (define (assv obj alist)
 
111
    (let loop ((rest alist))
 
112
      (cond ((null rest) #f)
 
113
            ((eql (caar rest) obj) (car rest))
 
114
            (t (loop (cdr rest))))))
 
115
 
 
116
;;; symbols
 
117
 
 
118
  (define (symbol? arg)
 
119
    (cond ((memq arg '(#f #t)) #f)
 
120
          ((symbolp arg) #t)
 
121
          (t #f)))
 
122
    
 
123
  (define symbol->string symbol-name)
 
124
 
 
125
  (define (string->symbol name)
 
126
    (if (string= name "nil")
 
127
        'nil
 
128
      (or (find-symbol name)
 
129
          ;; The copying is needed to pass test.scm..
 
130
          (intern (copy-sequence name)))))
 
131
 
 
132
;;; numbers
 
133
 
 
134
  (define number? numberp)
 
135
  (define (complex? obj)
 
136
    (declare (unused obj))
 
137
    #t)
 
138
  (define real? realp)
 
139
  (define rational? rationalp)
 
140
  (define integer? integerp)
 
141
 
 
142
  (define exact? exactp)
 
143
  (define inexact? inexactp)
 
144
 
 
145
  (define zero? zerop)
 
146
  (define positive? positivep)
 
147
  (define negative? negativep)
 
148
  (define odd? oddp)
 
149
  (define even? evenp)
 
150
 
 
151
  (define (rationalize x y)
 
152
    (declare (unused x y))
 
153
    (error "rationalize is unimplemented"))
 
154
 
 
155
;;; characters
 
156
 
 
157
  (define char? fixnump)
 
158
 
 
159
  (define char=? =)
 
160
  (define char<? <)
 
161
  (define char>? >)
 
162
  (define char<=? <=)
 
163
  (define char>=? >=)
 
164
 
 
165
  ;; XXX slow...
 
166
  (define (char-ci=? . args) (apply char=? (mapcar char-upcase args)))
 
167
  (define (char-ci<? . args) (apply char<? (mapcar char-upcase args)))
 
168
  (define (char-ci>? . args) (apply char>? (mapcar char-upcase args)))
 
169
  (define (char-ci<=? . args) (apply char<=? (mapcar char-upcase args)))
 
170
  (define (char-ci>=? . args) (apply char>=? (mapcar char-upcase args)))
 
171
 
 
172
  (define char-alphabetic? alpha-char-p)
 
173
  (define char-numeric? digit-char-p)
 
174
  (define char-whitespace? space-char-p)
 
175
  (define char-upper-case? upper-case-p)
 
176
  (define char-lower-case? lower-case-p)
 
177
 
 
178
  (define char->integer identity)
 
179
  (define integer->char identity)
 
180
 
 
181
;;; strings
 
182
 
 
183
  (define string? stringp)
 
184
 
 
185
  (define string concat)
 
186
  (define string-length length)
 
187
  (define string-ref aref)
 
188
  (define string-set! aset)
 
189
 
 
190
  (define string=? =)
 
191
  (define string<? <)
 
192
  (define string>? >)
 
193
  (define string<=? <=)
 
194
  (define string>=? >=)
 
195
 
 
196
  (define string-ci=? string-equal)
 
197
  (define string-ci<? string-lessp)
 
198
  (define string-ci>? (lambda args
 
199
                        (not (or (apply string-equal args)
 
200
                                 (apply string-lessp args)))))
 
201
  (define string-ci<=? (lambda args
 
202
                         (or (apply string-lessp args)
 
203
                             (apply string-equal args))))
 
204
  (define string-ci>=? (lambda args
 
205
                         (not (apply string-lessp args))))
 
206
 
 
207
  (define string-append concat)
 
208
 
 
209
  (define (string->list string)
 
210
    (let loop ((i (1- (length string)))
 
211
               (out nil))
 
212
      (if (>= i 0)
 
213
          (loop (1- i) (cons (aref string i) out))
 
214
        out)))
 
215
 
 
216
  (define list->string concat)
 
217
 
 
218
  (define string-copy copy-sequence)
 
219
 
 
220
  (define (string-fill! string char)
 
221
    (let loop ((i (1- (length string))))
 
222
      (cond ((>= i 0)
 
223
             (aset string i char)
 
224
             (loop (1- i))))))
 
225
 
 
226
;;; vectors
 
227
 
 
228
  (define vector? vectorp)
 
229
 
 
230
  (define vector-length length)
 
231
  (define vector-ref aref)
 
232
  (define vector-set! aset)
 
233
 
 
234
  (define (vector->list vect)
 
235
    (let loop ((i (1- (length vect)))
 
236
               (out nil))
 
237
      (if (>= i 0)
 
238
          (loop (1- i) (cons (aref vect i) out))
 
239
        out)))
 
240
 
 
241
  (define (list->vector lst)
 
242
    (apply vector lst))
 
243
 
 
244
  (define (vector-fill! vect char)
 
245
    (let loop ((i (1- (length vect))))
 
246
      (cond ((>= i 0)
 
247
             (aset vect i char)
 
248
             (loop (1- i)))))))