From: Martin DeMello
Subject: [newbie] mapping with side effects
Date: 
Message-ID: <k0YYd.664114$6l.370488@pd7tw2no>
The following code is an attempt to permute 2x2 blocks within a 2xn
array; all the individual pieces seem to be working, but the final call
to permute doesn't do anything.

Here's the code:

(defparameter *line* (make-array '( 2 16)))

(defun setl (i j val)
  (setf (aref *line* i j) val))

(defun getl (i j)
  (aref *line* i j))

(defun initline-improper ()
  (dotimes (i 2)
    (dotimes (j 16)
      (setl i j (mod (+ i j) 2)))))

(defun initline-proper ()
  (dotimes (i 2)
    (dotimes (j 16)
      (setl i j (mod i 2)))))

(defun offset (k)
  (let ((x (- k 1)))
    (multiple-value-list (floor x 2))))

(defun index-into (i j k)
  (mapcar '+ (list i j) (offset k)))

(defun get-at (i j k)
  (apply #'getl (index-into i j k)))

(defun set-at (i j k v)
  (apply #'setl (append (index-into i j k) (list v))))

(defun permute (i j l)
  (let ((temp (mapcar #'(lambda (x) (get-at i j x)) l)))
    (mapc #'(lambda (k v) (set-at i j k v)) l temp)))

And here's my REPL session:    

CL-USER> (initline-proper)
NIL
CL-USER> *line*
#2A((0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1))
CL-USER> (permute 0 0 '(4 3 2 1))
(4 3 2 1)
CL-USER> *line*
#2A((0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) (1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1))

I'd have expected the final *line* to be 

#2A((1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0) (0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1))

martin

From: Barry Margolin
Subject: Re: [newbie] mapping with side effects
Date: 
Message-ID: <barmar-C292BC.13252613032005@comcast.dca.giganews.com>
In article <······················@pd7tw2no>,
 Martin DeMello <·············@yahoo.com> wrote:

> (defun permute (i j l)
>   (let ((temp (mapcar #'(lambda (x) (get-at i j x)) l)))
>     (mapc #'(lambda (k v) (set-at i j k v)) l temp)))

It looks to me like you're just putting the values in TEMP back in the 
same places you got them from.  If you want to permute, shouldn't there 
be some difference in the arguments you pass to SET-AT than GET-AT?

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
From: Kalle Olavi Niemitalo
Subject: Re: [newbie] mapping with side effects
Date: 
Message-ID: <87br9nl9hq.fsf@Astalo.kon.iki.fi>
Martin DeMello <·············@yahoo.com> writes:

> (defun permute (i j l)
>   (let ((temp (mapcar #'(lambda (x) (get-at i j x)) l)))
>     (mapc #'(lambda (k v) (set-at i j k v)) l temp)))

As far as I can tell, this first reads values from *LINE*, at
the offsets given in the list L, measured from (I, J); and then
writes the values back to their original locations.

Perhaps either the reading or writing phase should use an
increasing list (1 2 3 4) instead of L.  Which one you choose
controls whether (2 4 1 3) is a clockwise or counterclockwise
rotation.
From: Martin DeMello
Subject: Re: [newbie] mapping with side effects
Date: 
Message-ID: <mc6Zd.670701$6l.468934@pd7tw2no>
Kalle Olavi Niemitalo <···@iki.fi> wrote:
> Martin DeMello <·············@yahoo.com> writes:
> 
> > (defun permute (i j l)
> >   (let ((temp (mapcar #'(lambda (x) (get-at i j x)) l)))
> >     (mapc #'(lambda (k v) (set-at i j k v)) l temp)))
> 
> As far as I can tell, this first reads values from *LINE*, at
> the offsets given in the list L, measured from (I, J); and then
> writes the values back to their original locations.

Doh! Stupid mistake indeed - thanks.

martin