From: ··············@web.de
Subject: problem with lamda/mapping function
Date: 
Message-ID: <1178887858.800144.297950@y5g2000hsa.googlegroups.com>
Hi,

I just wrote a function to create a contingency table. The binary data
looks like:
0 0 1 0 1
1 0 1 1 1

My code:

(defun X (l dim len)
(let ((ct (make-array dim :initial-element 0 )))
  (apply #'map nil
		 (append (list #'(lambda ( idx1 idx2)
						   (incf (apply #'aref ct idx)(/ 1 len))))
				 (coerce (car l) 'list)   (cdr l) ))
  ct))

A call of the function could be : (X  '(#(0 0 1 0 1) #(1 0 1 1 1))
'(2 2)    1)

But the compiler finds an error: "The value 0 is not of type
Sequence". Does anybody see the error?

Furthermore I want to create a contingency table with different "time
steps", i.e.
(1)
0 0 1 0 1
   1 0 1 1 1
--> (2,1)(1,0)

(2)
instead of
0 0 1 0 1
1 0 1 1 1
--> (2,0) (1,2)

Any ideas to realize it with my code written above? It is possible to
limit or control the number of iterations in a mapping function (in
(1) I need only 4 iterations, in (2) 5).

Thanks!

From: Dan Bensen
Subject: Re: problem with lamda/mapping function
Date: 
Message-ID: <f2211h$5ja$1@wildfire.prairienet.org>
··············@web.de wrote:
> (defun X (l dim len)
> (let ((ct (make-array dim :initial-element 0 )))
>   (apply #'map nil
> 		 (append (list #'(lambda ( idx1 idx2)
> 						   (incf (apply #'aref ct idx)(/ 1 len))))
> 				 (coerce (car l) 'list)   (cdr l) ))
>   ct))
> 
> A call of the function could be : (X  '(#(0 0 1 0 1) #(1 0 1 1 1))
> '(2 2)    1)

Your code as posted introduces idx without defining it and does
nothing with idx1 and idx2.  It also doesn't coerce the second array
in l.  After fixing those things, I got the same error you did.
I'm not sure what's wrong, but this at least runs:

(defun X (l dims len)
   (let ((ct (make-array dims :initial-element 0 )))
     (flet
      ((foo (idx1 idx2)
	   (incf (aref ct idx1 idx2) (/ 1 len))))
      (map nil
	  #'foo
	  (coerce (car  l) 'list)
	  (coerce (cadr l) 'list)))
     ct))

=> #2A((1 2) (0 2))

-- 
Dan
www.prairienet.org/~dsb/
From: Kent M Pitman
Subject: Re: problem with lamda/mapping function
Date: 
Message-ID: <uveez2xi1.fsf@nhplace.com>
Dan Bensen <··········@cyberspace.net> writes:

>       (map nil
> 	  #'foo
> 	  (coerce (car  l) 'list)
> 	  (coerce (cadr l) 'list)))

Are you sure you need those coerce calls there?  I'd be surprised if a
coerce call was ever needed for the third and subsequent args to MAP.

MAP (unlike other MAPxxx functions) is a "sequence function", that is,
it works directly on any kind of sequence, not just lists.  In fact,
that's the reason you have to tell it what kind of sequence you want back.

(map 'list #'cons #(a b c) "abc")
=> ((A . #\a) (B . #\b) (C . #\c))

(map 'vector #'cons #(a b c) "abc")
=> #((A . #\a) (B . #\b) (C . #\c))

(map 'string #'char-upcase #(#\a #\b #\c))
=> "ABC"

(map 'string #'code-char (map 'vector #'char-code "abc"))
=> "abc"
From: Dan Bensen
Subject: Re: problem with lamda/mapping function
Date: 
Message-ID: <f2229l$5vv$1@wildfire.prairienet.org>
Kent M Pitman wrote:
> Dan Bensen <··········@cyberspace.net> writes:
>> 	  (coerce (car  l) 'list)
>> 	  (coerce (cadr l) 'list)))
> Are you sure you need those coerce calls there?  I'd be surprised if a
> coerce call was ever needed for the third and subsequent args to MAP.

Thanks for pointing that out.  Since you gave me an excuse to repost,
here's a version without the flet:

(defun X (l dims len)
   (let ((ct (make-array dims :initial-element 0 )))
     (map nil
	 (lambda (idx1 idx2) (incf (aref ct idx1 idx2) (/ 1 len)))
	 (car  l)
	 (cadr l))
     ct))

-- 
Dan
www.prairienet.org/~dsb/
From: Rainer Joswig
Subject: Re: problem with lamda/mapping function
Date: 
Message-ID: <joswig-D539A8.14594811052007@news-europe.giganews.com>
In article <························@y5g2000hsa.googlegroups.com>,
 ··············@web.de wrote:

> Hi,
> 
> I just wrote a function to create a contingency table. The binary data
> looks like:
> 0 0 1 0 1
> 1 0 1 1 1
> 
> My code:
> 
> (defun X (l dim len)
> (let ((ct (make-array dim :initial-element 0 )))
>   (apply #'map nil
> 		 (append (list #'(lambda ( idx1 idx2)
> 						   (incf (apply #'aref ct idx)(/ 1 len))))
> 				 (coerce (car l) 'list)   (cdr l) ))
>   ct))
> 
> A call of the function could be : (X  '(#(0 0 1 0 1) #(1 0 1 1 1))
> '(2 2)    1)

What should the output be?
> 
> But the compiler finds an error: "The value 0 is not of type
> Sequence". Does anybody see the error?
> 
> Furthermore I want to create a contingency table with different "time
> steps", i.e.
> (1)
> 0 0 1 0 1
>    1 0 1 1 1
> --> (2,1)(1,0)
> 
> (2)
> instead of
> 0 0 1 0 1
> 1 0 1 1 1
> --> (2,0) (1,2)
> 
> Any ideas to realize it with my code written above? It is possible to
> limit or control the number of iterations in a mapping function (in
> (1) I need only 4 iterations, in (2) 5).
> 
> Thanks!

-- 
http://lispm.dyndns.org
From: Rainer Joswig
Subject: Re: problem with lamda/mapping function
Date: 
Message-ID: <joswig-F29BC9.15063211052007@news-europe.giganews.com>
In article <························@y5g2000hsa.googlegroups.com>,
 ··············@web.de wrote:

> Hi,
> 
> I just wrote a function to create a contingency table. The binary data
> looks like:
> 0 0 1 0 1
> 1 0 1 1 1
> 
> My code:
> 
> (defun X (l dim len)
> (let ((ct (make-array dim :initial-element 0 )))
>   (apply #'map nil
> 		 (append (list #'(lambda ( idx1 idx2)
> 						   (incf (apply #'aref ct idx)(/ 1 len))))
> 				 (coerce (car l) 'list)   (cdr l) ))
>   ct))

APPLY is almost never needed. 

This does something, but I'm not sure what you want as an output...

(defun X (l dim len)
  (let ((ct (make-array dim :initial-element 0 )))
    (map nil
         #'(lambda (idx1 idx2)
             (incf (aref ct idx1 idx2)
                   (/ 1 len)))
         (first l)
         (second l))
    ct))

> 
> A call of the function could be : (X  '(#(0 0 1 0 1) #(1 0 1 1 1))
> '(2 2)    1)
> 
> But the compiler finds an error: "The value 0 is not of type
> Sequence". Does anybody see the error?
> 
> Furthermore I want to create a contingency table with different "time
> steps", i.e.
> (1)
> 0 0 1 0 1
>    1 0 1 1 1
> --> (2,1)(1,0)
> 
> (2)
> instead of
> 0 0 1 0 1
> 1 0 1 1 1
> --> (2,0) (1,2)
> 
> Any ideas to realize it with my code written above? It is possible to
> limit or control the number of iterations in a mapping function (in
> (1) I need only 4 iterations, in (2) 5).
> 
> Thanks!

-- 
http://lispm.dyndns.org
From: ··············@web.de
Subject: Re: problem with lamda/mapping function
Date: 
Message-ID: <1178888966.908112.272420@h2g2000hsg.googlegroups.com>
> APPLY is almost never needed.
>
> This does something, but I'm not sure what you want as an output...
>
> (defun X (l dim len)
>   (let ((ct (make-array dim :initial-element 0 )))
>     (map nil
>          #'(lambda (idx1 idx2)
>              (incf (aref ct idx1 idx2)
>                    (/ 1 len)))
>          (first l)
>          (second l))
>     ct))

The output is a contincency table (the matrix ct) like:
(2,1)(1,0) for the example:
 0 0 1 0 1
   1 0 1 1 1
From: Rainer Joswig
Subject: Re: problem with lamda/mapping function
Date: 
Message-ID: <joswig-AFAD7C.16172911052007@news-europe.giganews.com>
In article <························@h2g2000hsg.googlegroups.com>,
 ··············@web.de wrote:

> > APPLY is almost never needed.
> >
> > This does something, but I'm not sure what you want as an output...
> >
> > (defun X (l dim len)
> >   (let ((ct (make-array dim :initial-element 0 )))
> >     (map nil
> >          #'(lambda (idx1 idx2)
> >              (incf (aref ct idx1 idx2)
> >                    (/ 1 len)))
> >          (first l)
> >          (second l))
> >     ct))
> 
> The output is a contincency table (the matrix ct) like:
> (2,1)(1,0) for the example:
>  0 0 1 0 1
>    1 0 1 1 1

You can experiment with this LOOP-based version:

(defun X (l dim)
  (let ((ct (make-array dim :initial-element 0 )))
    (destructuring-bind (v0 v1) l
      (loop for i0 from 0 below (length v0)
            for i1 from 0 below (length v1)
            do (incf (aref ct
                           (aref v0 i0)
                           (aref v1 i1)))))
    ct))

-- 
http://lispm.dyndns.org