Skip to content

Commit

Permalink
dynamic [] overload
Browse files Browse the repository at this point in the history
  • Loading branch information
damien-mattei committed Sep 12, 2023
1 parent 636db7d commit d526445
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions array.scm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@
((_ array lin col val)
(vector-set! (vector-ref array lin) col val))))

;; Scheme+ version
;; create a vector of line and column with a function
;; (define (create-vector-2d fct lin col)
;; {v <+ (make-array-2d lin col)}
;; (for ({l <+ 0} {l < lin} {l <- l + 1})
;; (for ({c <+ 0} {c < col} {c <- c + 1})
;; {v[l][c] <- (fct l c)}))
;; v)

;; create a vector (or array) of line and column with a function
(define (create-vector-2d fct lin col)
Expand Down
20 changes: 19 additions & 1 deletion overload.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@



;; args must be the same number as predicates and their types must match
;; args must be the same number as predicates and their types must match
;; (check-arguments '() '())
;; #t
(define (check-arguments pred-list args)
;;(display "pred-list=") (display pred-list) (newline)
;;(display "args=")(display args) (newline)
Expand Down Expand Up @@ -766,6 +768,22 @@
;; > {Mv[1 0]}
;; 4

;; > (overload-square-brackets matrix-vect-ref matrix-vect-set! (matrix-vect? number? number?))
;; > (define Mv (matrix-vect (vector (vector 1 2 3) (vector 4 5 6))))
;; > (matrix-vect-v Mv)
;; '#(#(1 2 3) #(4 5 6))
;; > {Mv[1 0] <- -7}
;; -7
;; > (matrix-vect-v Mv)
;; '#(#(1 2 3) #(-7 5 6))
;; > {Mv[1][0] <- 10}
;; . . ../../Scheme-PLUS-for-Racket/main/Scheme-PLUS-for-Racket/overload.rkt:820:13: $bracket-apply$: no matching found in $ovrld-square-brackets-lst$ : failed with those arguments list (#<matrix-vect> 1)
;; > (overload-square-brackets matrix-vect-line-ref matrix-vect-line-set! (matrix-vect? number?))
;; > {Mv[1][0] <- 10}
;; 10
;; > (matrix-vect-v Mv)
;; '#(#(1 2 3) #(10 5 6))

(define-syntax overload-square-brackets

(syntax-rules ()
Expand Down

0 comments on commit d526445

Please sign in to comment.