Problems with "count"

Started by alex, November 18, 2005, 11:38:07 PM

Previous topic - Next topic

alex

Problems with "count"



We have:

G:MAINQM_NEWLISPnewlisp.bin>newlisp
newLISP v.8.7.1 on Win32 MinGW, execute 'newlisp -h' for more info.

> (count '(1 2 1 2) '(1 2 1 2))
(2 2 0 0)

Why not (2 2 2 2) ?



Moreover, code



G:MAINQM_NEWLISPnewlisp.bin>newlisp
newLISP v.8.7.1 on Win32 MinGW, execute 'newlisp -h' for more info.

> (setq li '(1 2 1 2 3))
(1 2 1 2 3)
> (count li li)

G:MAINQM_NEWLISPnewlisp.bin>

crash my newlisp!

I use Windows XP SP2

What You think about it?

HPW

#1
> (count '(1 2 1 2) '(1 2 1 2))



Why do you want to count an element twice?



(count '(1 2) '(1 2 1 2))



or



(count (unique '(1 2 1 2)) '(1 2 1 2))



(setq li '(1 2 1 2 3))

(count (unique li)li)



Of cource it is not documented if it is allowed to use duplicates in first list.

So it may be a bug.



The crash might come from that count is destruktive on its copy of the list and pop the counted value from the list. When the list is nil it crashes.



(setq li '(1 2 1 2 3))

(count '(1 2 1 2 3) li)=> (2 2 0 0 1)



This works. So the first list is not allowd to change during count.
Hans-Peter

alex

#2
To HPW



1.Yes, I want count an element twice. Why I can't do it?



2. According to newlisp manual "count" is not destruktive. May be manual is not full?

HPW

#3
>1.Yes, I want count an element twice. Why I can't do it?



Lutz has to answer.



>2. According to newlisp manual "count" is not destruktive. May be manual is not full?



As I wrote before, it is destruktive on its copy of the list. So when both parameter points to the same list, then the first parameter gets changed during count.



(setq li '(1 2 1 2 3))

(count '(1 2 1 2 3) li)=> (2 2 0 0 1)

li

(1 2 1 2 3)



You see that it is non-destruktive on li.
Hans-Peter

Lutz

#4
The first list of 'count' should be unique, if not the first occurence will pull the total count and leave the rest zero, becuase internally 'count' sorts both lists and then steps sequentially through them once counting.



In the next development version 'count', 'difference' and 'intersect' will handle the case of the list beeing the same correctly.



Lutz



ps: 8.7.2 is due this weekend

alex

#5
Thank You, Lutz!

I wait new version for testing :)