Welcome to the CHICKEN Scheme pasting service

SRFI-17 setter for coops method pasted by Saeren on Tue Jun 12 04:43:10 2012

;;; srfi-17 setter for single element in a matrix -- this works
(define-method ((setter elem) (mat <matrix>) (i <fixnum>) (j <fixnum>) val)
  (let ((idx (+ i (vector-ref (index mat) j))))
    (f64vector-set! (data mat) idx val)))

;;; simple setter to change some specified column to val -- this works
(define (column-set! mat col val)
  (if (not (= (f64vector-length val) (nrow mat)))
      (error "val not same length as matrix columns")
      (for-each
       (lambda (i) (set! (elem mat i col) (f64vector-ref val i)))
       (iota (f64vector-length val)))))

;;; srfi-17 setter for the column -- this does not work, returns the matrix but
;;; does not have side effects
(define-method ((setter column) (mat <matrix>) (col <fixnum>) val)
  (if (not (= (f64vector-length val) (nrow mat)))
      (error "val not same length as matrix columns")
      (for-each
       (lambda (i) (set! (elem mat i col) (f64vector-ref val i)))
       (iota (f64vector-length val)))))


;;; Simple example

;;; define matrix
(define X (matrix '#f64(1 2 3 4 1 1 1 1 3 4 5 6 5 6 7 8) 4))

;;; replace column 2 with new values
(set! (column X 2) (f64vector 1 2 3 4))

Two things to help diagnose the problem added by jacius on Tue Jun 12 05:01:42 2012

;;; coops, no srfi-17
(define-method (column-set! (mat <matrix>) (col <fixnum>) val)
  (if (not (= (f64vector-length val) (nrow mat)))
      (error "val not same length as matrix columns")
      (for-each
       (lambda (i) (set! (elem mat i col) (f64vector-ref val i)))
       (iota (f64vector-length val)))))

;;; srfi-17, no coops
(define ((setter column) mat col val)
  (if (not (= (f64vector-length val) (nrow mat)))
      (error "val not same length as matrix columns")
      (for-each
       (lambda (i) (set! (elem mat i col) (f64vector-ref val i)))
       (iota (f64vector-length val)))))

Your annotation:

Enter a new annotation:

Your nick:
The title of your paste:
Your paste (mandatory) :
Name of the language CHICKEN compiles to:
Visually impaired? Let me spell it for you (wav file) download WAV