Problems with "count"

Q&A's, tips, howto's
Locked
alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Problems with "count"

Post 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?

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post 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.
Hans-Peter

alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post 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?

HPW
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post 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.
Hans-Peter

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post by alex »

Thank You, Lutz!
I wait new version for testing :)

Locked