Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; A grid is is an abstract object that holds `items` at each
- ;;; `coordinate`. In particular, n x m grid consists of lattice points
- ;;; (x,y) where 0 ≤ x < n and 0 ≤ y < m.
- ;;; At each coordinate (x,y) contains an object.
- (define (make-grid n m)
- ;; add a test whether n ≥ 1 and m ≥ 1
- (let ([grid (make-vector (* m n) '())])
- (define (position->index i j)
- (+ j (* i m)))
- (define (get-coordinates i j)
- (vector-ref grid (position->index i j)))
- (define (get-self) grid)
- (define (populate-with f)
- (for* ([i n]
- [j m])
- (vector-set! grid (position->index i j) (f i j))))
- (define (set-coordinates i j val)
- (vector-set! grid (position->index i j) val))
- (case-lambda
- [(cmd)
- (cond
- [(eq? cmd 'get) grid]
- [else (error "make-grid, unknown command")])]
- ;;
- [(i j)
- (cond
- [(and (integer? i) (integer? j)) (get-coordinates i j)]
- [(and (eq? i 'populate-using) (procedure? j)) (populate-with j)]
- [(eq? i 'populate-constant) (populate-with (lambda (x y) j))])]
- [(i j val)
- (cond
- [(and (integer? i) (integer? j)) (set-coordinates i j val)])]
- )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement