Page 1 of 1

Problems with "count"

Posted: Sat Nov 19, 2005 7:38 am
by alex
Problems with "count"

We have:

Code: Select all

G:\MAIN\QM\_NEWLISP\newlisp.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

Code: Select all

G:\MAIN\QM\_NEWLISP\newlisp.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:\MAIN\QM\_NEWLISP\newlisp.bin>
crash my newlisp!
I use Windows XP SP2
What You think about it?

Posted: Sat Nov 19, 2005 8:41 am
by HPW
> (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.

Posted: Sat Nov 19, 2005 12:11 pm
by alex
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?

Posted: Sat Nov 19, 2005 12:56 pm
by HPW
>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.

Posted: Sat Nov 19, 2005 1:20 pm
by Lutz
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

Posted: Sat Nov 19, 2005 3:53 pm
by alex
Thank You, Lutz!
I wait new version for testing :)