Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Elementary but surprisingly difficult.
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 49 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Albert van der Horst  
View profile  
 More options May 24 2008, 4:05 pm
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 24 May 2008 20:05:48 GMT
Local: Sat, May 24 2008 4:05 pm
Subject: Elementary but surprisingly difficult.
During the Euler stuff I came upon this following sub-problem.
It is elementary enough to be in a library somewhere.
An area giving as (addr_start, addr_end) contains items
of length ``len''. I want to get of the duplicates.

Sorting the items is pretty easy, qsort can handle that.
Now eliminating the duplicates, we need only compare one
item to the next.
Something along compact (start, end, len -- end' )
The new end should be returned, of course.

Even using variables/locals, I find this difficult.
(More difficult then e.g. making a table for the number of
dividers of N, which sounds much less trivial.)

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Slava Pestov  
View profile  
 More options May 24 2008, 5:02 pm
Newsgroups: comp.lang.forth
From: Slava Pestov <sl...@jedit.org>
Date: Sat, 24 May 2008 14:02:20 -0700 (PDT)
Local: Sat, May 24 2008 5:02 pm
Subject: Re: Elementary but surprisingly difficult.
On May 24, 3:05 pm, Albert van der Horst <alb...@spenarnc.xs4all.nl>
wrote:

> Even using variables/locals, I find this difficult.
> (More difficult then e.g. making a table for the number of
> dividers of N, which sounds much less trivial.)

It's not difficult if you have a library of sequence operations:

http://factor-language.blogspot.com/2008/05/removing-duplicates-from-...

Slava


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bernd Paysan  
View profile  
 More options May 24 2008, 5:36 pm
Newsgroups: comp.lang.forth
From: Bernd Paysan <bernd.pay...@gmx.de>
Date: Sat, 24 May 2008 23:36:14 +0200
Local: Sat, May 24 2008 5:36 pm
Subject: Re: Elementary but surprisingly difficult.

Slava Pestov wrote:
> On May 24, 3:05 pm, Albert van der Horst <alb...@spenarnc.xs4all.nl>
> wrote:
>> Even using variables/locals, I find this difficult.
>> (More difficult then e.g. making a table for the number of
>> dividers of N, which sounds much less trivial.)

> It's not difficult if you have a library of sequence operations:

Nice filter, but doing that in-place is not difficult, either.

I give you a solution for characters in a string, and Albert can generalize
it. It is supposed that the string is sorted, so it will only remove
sequenes of the same character:

: uniquify ( addr u -- addr u' )
  over >r  bounds dup dup c@ 2swap 1+ ?DO
     I c@ tuck <> IF  swap 1+ 2dup c! swap  THEN  LOOP
  drop r> swap over - ;

--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://www.jwdt.com/~paysan/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Slava Pestov  
View profile  
 More options May 24 2008, 6:23 pm
Newsgroups: comp.lang.forth
From: Slava Pestov <sl...@jedit.org>
Date: Sat, 24 May 2008 15:23:29 -0700 (PDT)
Local: Sat, May 24 2008 6:23 pm
Subject: Re: Elementary but surprisingly difficult.
On May 24, 4:36 pm, Bernd Paysan <bernd.pay...@gmx.de> wrote:

> : uniquify ( addr u -- addr u' )
>   over >r  bounds dup dup c@ 2swap 1+ ?DO
>      I c@ tuck <> IF  swap 1+ 2dup c! swap  THEN  LOOP
>   drop r> swap over - ;

This is why stack-based languages have a reputation of unreadability.

Slava


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bruce McFarling  
View profile  
 More options May 24 2008, 7:24 pm
Newsgroups: comp.lang.forth
From: Bruce McFarling <agil...@netscape.net>
Date: Sat, 24 May 2008 16:24:38 -0700 (PDT)
Local: Sat, May 24 2008 7:24 pm
Subject: Re: Elementary but surprisingly difficult.
On May 24, 6:23 pm, Slava Pestov <sl...@jedit.org> wrote:

> On May 24, 4:36 pm, Bernd Paysan <bernd.pay...@gmx.de> wrote:

> > : uniquify ( addr u -- addr u' )
> >   over >r  bounds dup dup c@ 2swap 1+ ?DO
> >      I c@ tuck <> IF  swap 1+ 2dup c! swap  THEN  LOOP
> >   drop r> swap over - ;
> This is why stack-based languages have a reputation of unreadability.

``uniquify'' ... I dunno, that word looks pretty readable to me.

Its *definition* is underfactored, of course, and underfactored
definitions are always hard to read, but

uniquify ( addr u -- addr u' )
    trim duplicate characters from a sorted string, in place

... seems pretty readable to me.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
mark4  
View profile  
 More options May 24 2008, 8:08 pm
Newsgroups: comp.lang.forth
From: mark4 <i4...@mailcity.com>
Date: Sat, 24 May 2008 17:08:52 -0700 (PDT)
Local: Sat, May 24 2008 8:08 pm
Subject: Re: Elementary but surprisingly difficult.
On May 24, 1:05 pm, Albert van der Horst <alb...@spenarnc.xs4all.nl>
wrote:

I didnt test this but it looks right (dont take my word for it
though :)

assumes string is 0 terminated though, which is most un-forthlike :P

: remove-dups       ( a1 --- )
  dup
  begin
    begin
      1+
      2dup c@ swap c@ <>
    until
    swap 1+ swap
    2dup c@ dup rot c!
    0=
  until ;


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
van...@vsta.org  
View profile  
 More options May 24 2008, 8:11 pm
Newsgroups: comp.lang.forth
From: van...@vsta.org
Date: 25 May 2008 00:11:30 GMT
Local: Sat, May 24 2008 8:11 pm
Subject: Re: Elementary but surprisingly difficult.

Bruce McFarling <agil...@netscape.net> wrote:
>> This is why stack-based languages have a reputation of unreadability.
> ...
> Its *definition* is underfactored, of course, and underfactored
> definitions are always hard to read, ...

So, could you try your hand at a properly factored definition.  This is
a good example of the kind of thing I never code cleanly in Forth.

Thanks,
Andy Valencia


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Doug Hoffman  
View profile  
 More options May 24 2008, 9:18 pm
Newsgroups: comp.lang.forth
From: Doug Hoffman <no.spam>
Date: Sat, 24 May 2008 21:18:17 -0400
Local: Sat, May 24 2008 9:18 pm
Subject: Re: Elementary but surprisingly difficult.

Slava Pestov wrote:
> On May 24, 4:36 pm, Bernd Paysan <bernd.pay...@gmx.de> wrote:
>> : uniquify ( addr u -- addr u' )
>>   over >r  bounds dup dup c@ 2swap 1+ ?DO
>>      I c@ tuck <> IF  swap 1+ 2dup c! swap  THEN  LOOP
>>   drop r> swap over - ;

> This is why stack-based languages have a reputation of unreadability.

I both agree and disagree with you in this case.  It is not the kind of
code I would normally write, especially if in a hurry to get the job
done.  Of course I could spend the time to pick it apart and see exactly
what is going.  But why bother?  The word works.  Some people, like
Bernd, can probably glance at code like this and easily/quickly see
exactly what makes it tick.  I am not one of those people, even though I
like and use Forth.

I will hang on to 'uniquify' because I can see where it could be useful
to me in the future.  (Thanks Bernd!).

-Doug


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Doug Hoffman  
View profile  
 More options May 24 2008, 10:52 pm
Newsgroups: comp.lang.forth
From: Doug Hoffman <dhoff...@talkamerica.net>
Date: Sat, 24 May 2008 19:52:43 -0700 (PDT)
Local: Sat, May 24 2008 10:52 pm
Subject: Re: Elementary but surprisingly difficult.
On May 24, 9:18 pm, Doug Hoffman <no.spam> wrote:

Having said that, I thought I should show the way I would normally
attack this kind of problem (context is resource-rich pc
emvironment).  The Mops Neon-based object library has some powerful
string handling methods.  So I would start with that and come up with
something like the following.  It may not be as readable to you as
your Factor solution.  But then for *me* this is far more readable
than the Factor code because I know nothing about Factor.

(Note that Mops string objects maintain 2 pointers into the string,
position and limit, or pos and lim.  Manipulation of these pointers is
the key to using string objects.)

:class ustring super{ string+ }
 :m 2nd: ( -- c) \ 2nd char
    1 skip: self  1st: self ;m
 :m del1: 1 deleten: self ;m
 :m backup1: pos: self 1 - >pos: self ;m
 :m unique:
    BEGIN
        pos: self 1 +  lim: self <
    WHILE
        1st: self  2nd: self =
        IF del1: self  backup1: self THEN
    REPEAT ;m
;class

ustring s

" abbcdefffghiijkkkkkkklmnooz" put: s
unique: s
all: s type
abcdefghijklmnoz ok

-Doug


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Passaniti  
View profile  
 More options May 25 2008, 1:54 am
Newsgroups: comp.lang.forth
From: John Passaniti <n...@JapanIsShinto.com>
Date: Sun, 25 May 2008 01:54:39 -0400
Local: Sun, May 25 2008 1:54 am
Subject: Re: Elementary but surprisingly difficult.
Albert van der Horst wrote:

> Sorting the items is pretty easy, qsort can handle that.
> Now eliminating the duplicates, we need only compare one
> item to the next.

What if the original ordering must be preserved?  By sorting, you've
destroyed that ordering.  You would now have to annotate each item so
that you could later sort again.  So the computational cost of this now
requires an annotation pass and two sorts... if the original ordering
must be preserved.

A less general way to deal with this would be to construct a parallel
data structure that recorded the values.  In the case where each item
was relatively small (say, a 16-bit integer or a 8-bit character), then
a bitmap is an obvious data structure.  As you move through the list of
items, check first if the item is set in the bitmap.  If it is, then
it's a duplicate.  If it isn't, then mark the bitmap.  No sorting is
necessary and duplicate detection occurs in a single pass.

As the size of the items being checked increases, storing it as a bitmap
gets more costly.  At that point, you can then using hashing (ideally
picking a hash algorithm that works well for the data set) or you can
use a bitmap representation that exploits any sparseness inherent in the
data.  In the case of hashing, storing hash collisions in a list would
be one strategy.  In the case of a sparse bitmap, a radix trie might
work well.

Removing the actual duplicates is a separate matter.  I would use two
pointers-- one for reading the current item, the other for where to
write it.  As long as both pointers are the same, there have been no
duplicates and thus no reason to do a write.  When the pointers are no
longer the same, then a write is needed.  This could be slightly more
intelligent by delaying the write until the next duplicate, allowing a
block move instead of an item-by-item move.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile  
 More options May 25 2008, 5:36 am
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 25 May 2008 09:36:32 GMT
Local: Sun, May 25 2008 5:36 am
Subject: Re: Elementary but surprisingly difficult.
In article <ebemg5-rae....@vimes.paysan.nom>,
Bernd Paysan  <bernd.pay...@gmx.de> wrote:

The main problem is that the content cannot be placed
on the stack they cannot be compared with <> etc.
The above solution foregoes the technical problem.
The algorithmic solution you gave is of course easy enough to come up
with. Although it looks ugly enough even for this simple case.

>--
>Bernd Paysan
>"If you want it done right, you have to do it yourself"
>http://www.jwdt.com/~paysan/

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile  
 More options May 25 2008, 5:39 am
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 25 May 2008 09:39:06 GMT
Local: Sun, May 25 2008 5:39 am
Subject: Re: Elementary but surprisingly difficult.
In article <d9ff9$4838be72$cdd0851a$13...@DIALUPUSA.NET>,
Doug Hoffman  <dhoff...@talkamerica.net> wrote:

No it doesn't work for me. It is hard to understand that using it
for what I want is very hard.
(Compare this to C :   pc++ , now pc point to a struct instead of
a char, no sweat).

>I will hang on to 'uniquify' because I can see where it could be useful
>to me in the future.  (Thanks Bernd!).

>-Doug

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile  
 More options May 25 2008, 5:42 am
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 25 May 2008 09:42:42 GMT
Local: Sun, May 25 2008 5:42 am
Subject: Re: Elementary but surprisingly difficult.
In article <ac0bbd8f-e6b1-443c-9d6e-bc7206845...@b9g2000prh.googlegroups.com>,

To be exact the array contains data structures representing
a circuit composed of identical condensors. It is 8 bytes, and
nothing must be assumed about the content. :-(

Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Passaniti  
View profile  
(1 user)  More options May 25 2008, 7:55 am
Newsgroups: comp.lang.forth
From: John Passaniti <put-my-first-name-h...@JapanIsShinto.com>
Date: Sun, 25 May 2008 07:55:37 -0400
Local: Sun, May 25 2008 7:55 am
Subject: Re: Elementary but surprisingly difficult.

Bruce McFarling wrote:
>> This is why stack-based languages have a reputation of unreadability.

> ``uniquify'' ... I dunno, that word looks pretty readable to me.

Wow!  This is truly an amazing breakthrough!  In the past, reading code
meant... reading code.  What a chore!  Worrying about balancing control
structures, thinking about how each word affects the stack, considering
how words might screw around with the input stream, etc.

But now, thanks to the awesome power of simply redefining English words
so they mean what we want, we've solved that problem.  Now, readability
is merely the ability to read the name of a definition and a comment
stating the purpose of the definition!


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile  
 More options May 25 2008, 7:46 am
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 25 May 2008 11:46:35 GMT
Local: Sun, May 25 2008 7:46 am
Subject: Re: Elementary but surprisingly difficult.
In article <8a7_j.3356$%T3....@fe105.usenetserver.com>,
John Passaniti  <n...@JapanIsShinto.com> wrote:

>Albert van der Horst wrote:
>> Sorting the items is pretty easy, qsort can handle that.
>> Now eliminating the duplicates, we need only compare one
>> item to the next.

>What if the original ordering must be preserved?  By sorting, you've
>destroyed that ordering.  You would now have to annotate each item so
>that you could later sort again.  So the computational cost of this now
>requires an annotation pass and two sorts... if the original ordering
>must be preserved.

Sorry dude, I have here a set problem at hand. The order is irrelevant,
but I can't afford to waste time on duplicates.
<SNIP>

Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile  
 More options May 25 2008, 7:43 am
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 25 May 2008 11:43:50 GMT
Local: Sun, May 25 2008 7:43 am
Subject: Re: Elementary but surprisingly difficult.
In article <ebemg5-rae....@vimes.paysan.nom>,
Bernd Paysan  <bernd.pay...@gmx.de> wrote:

----------------------------------------------
WANT CASE-INSENSITIVE     CASE-INSENSITIVE

WANT TUCK
WANT BOUNDS
WANT DUMP

CREATE AAP 1 C, 1 C, 2 C, 2 C, 3 C, 4 C, 5 C, 5 C, 5 C, 5 C,

: uniquify ( addr u -- addr u' )
  over >r  bounds dup dup c@ 2swap 1+ ?DO
     I c@ tuck <> IF  swap 1+ 2dup c! swap  THEN  LOOP
  drop r> swap over - ;

AAP 10 uniquify .

AAP 100 DUMP

----------------------------------------------

albert@apple:~/PROJECT/euler> lina -a
"unique.frt" INCLUDED
4
0805,124C:                               0102 0304 |....|
0805,1250: 0504 0505 0505 0000 0800 0000 756E 6971 |............uniq|
0805,1260: 7569 6679 68A7 0408 8012 0508 0000 0000 |uifyh...........|
0805,1270: 2C12 0508 5812 0508 0A05 0508 0000 0000 |,...X...........|
0805,1280: 98A3 0408 B8A1 0408 740A 0508 48A4 0408 |........t...H...|
0805,1290: 48A4 0408 98A5 0408 A8A4 0408 E4AF 0408 |H...............|
0805,12A0: 2C99 0408 3400 0000 6899 0408 98A5 0408 |,...4...h.......|
 OK
BYE

----------------------------------------------

So is this a bug in lina, or does it fail the first test
I throw at it?
(I expect 5 of course.)

The code is not transparent enough that it would be of
any value as specification of an algorithm, or in this
case to debug.

>--
>Bernd Paysan

Groetjes Albert

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile  
 More options May 25 2008, 7:52 am
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 25 May 2008 11:52:29 GMT
Local: Sun, May 25 2008 7:52 am
Subject: Re: Elementary but surprisingly difficult.
In article <8a7_j.3356$%T3....@fe105.usenetserver.com>,
John Passaniti  <n...@JapanIsShinto.com> wrote:
<SNIP>

>Removing the actual duplicates is a separate matter.  I would use two
>pointers-- one for reading the current item, the other for where to
>write it.  As long as both pointers are the same, there have been no
>duplicates and thus no reason to do a write.  When the pointers are no
>longer the same, then a write is needed.  This could be slightly more
>intelligent by delaying the write until the next duplicate, allowing a
>block move instead of an item-by-item move.

That is a good description and much in line with my thoughts.
Programming it in Forth is a different matter.
I started with telling that I have technical difficulties.

Groetjes Albert.

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert van der Horst  
View profile  
 More options May 25 2008, 9:40 am
Newsgroups: comp.lang.forth
From: Albert van der Horst <alb...@spenarnc.xs4all.nl>
Date: 25 May 2008 13:40:27 GMT
Local: Sun, May 25 2008 9:40 am
Subject: Re: Elementary but surprisingly difficult.
In article <k1fabh....@spenarnc.xs4all.nl>,
Albert van der Horst  <alb...@spenarnc.xs4all.nl> wrote:

>In article <8a7_j.3356$%T3....@fe105.usenetserver.com>,
>John Passaniti  <n...@JapanIsShinto.com> wrote:
><SNIP>

>>Removing the actual duplicates is a separate matter.  I would use two
>>pointers-- one for reading the current item, the other for where to
>>write it.  As long as both pointers are the same, there have been no
>>duplicates and thus no reason to do a write.  When the pointers are no
>>longer the same, then a write is needed.  This could be slightly more
>>intelligent by delaying the write until the next duplicate, allowing a
>>block move instead of an item-by-item move.

>That is a good description and much in line with my thoughts.
>Programming it in Forth is a different matter.
>I started with telling that I have technical difficulties.

I did something I didn't want to do.
I wrote it in C first then I translated it to Forth:
I could write the c-code almost without thinking in mere minutes.
Mechanically translating to Forth was also mere minutes.

-----------------------------------------

sometype *unique( sometype  aap[], int len )
{
   sometype *ps; sometype *pd;

   for (pd=ps=aap; ps<aap[len]; pd++,ps++)
   {
       while( ps <aap[len-1] && ps[0] == ps[1] ) ps++;
       *pd = *ps; /* strncpy( pd, ps ,sizeof (sometype)); */
   }
   return pd+1;

}

\ Sort from BEGIN to END. Leave new END   (adr1, adr2 -- adr3)
:  doit  >R
   begin DUP ( pd ps)
       DUP R@ <  WHILE
       BEGIN DUP CELL+ R@ < IF DUP @ OVER CELL+ @ = ELSE 0 THEN WHILE
            CELL+
       REPEAT
       SWAP   OVER @ OVER !
       CELL+ SWAP CELL+
   REPEAT
   CELL+
;

-----------------------------------------

Neither of the above code was debugged.
Debugging the Forth code, was relatively easy:

---------------------------------------
:  doit  >R
   DUP
   BEGIN ( pd ps)
       DUP R@ <  WHILE
       BEGIN DUP CELL+ R@ < IF DUP @ OVER CELL+ @ .S = ELSE 0 THEN .S WHILE
           CELL+
       REPEAT
       SWAP   OVER @ OVER !
       CELL+ SWAP CELL+
   REPEAT  DROP RDROP
;

CREATE AAP 1 , 1 , 2 , 2 , 3 , 4 , 5 , 5 , 5 , 5 ,

AAP DUP 10 CELLS +
.S
doit
.S
CR
AAP - 1 CELLS /MOD . .
CR
AAP 110 DUMP

------------------------------------------

This code passed.

Now it turns out, as usual, that I didn't need this in the first
place. :-(.

Groetjes Albert.

>Groetjes Albert.

>--
>--
>Albert van der Horst, UTRECHT,THE NETHERLANDS
>Economic growth -- like all pyramid schemes -- ultimately falters.
>albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bernd Paysan  
View profile  
(1 user)  More options May 25 2008, 2:59 pm
Newsgroups: comp.lang.forth
From: Bernd Paysan <bernd.pay...@gmx.de>
Date: Sun, 25 May 2008 20:59:24 +0200
Local: Sun, May 25 2008 2:59 pm
Subject: Re: Elementary but surprisingly difficult.
Albert van der Horst wrote:

> The main problem is that the content cannot be placed
> on the stack they cannot be compared with <> etc.

I don't see the big problem. If you can't place the content on the stack,
place a pointer. And compare with whatever compare function you need. If
you want this a library function like qsort, define that your array is an
array of pointers to the actual structures, and that you have a deferred
word for the actual comparison. This will compare for <= (whatever that is
for your objects), and is used for both the qsort and the duplicate
removal. You just have to make sure that the comparison order is the right
one, i.e. if you expect a to be <= b, compare b <= a for duplicate removal
(will only be true if b=a).

--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://www.jwdt.com/~paysan/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bernd Paysan  
View profile  
 More options May 26 2008, 3:25 am
Newsgroups: comp.lang.forth
From: Bernd Paysan <bernd.pay...@gmx.de>
Date: Mon, 26 May 2008 09:25:18 +0200
Local: Mon, May 26 2008 3:25 am
Subject: Re: Elementary but surprisingly difficult.
Albert van der Horst wrote:

> : uniquify ( addr u -- addr u' )
>   over >r  bounds dup dup c@ 2swap 1+ ?DO
>      I c@ tuck <> IF  swap 1+ 2dup c! swap  THEN  LOOP
>   drop r> swap over - ;

> AAP 10 uniquify .

[gives 4]

Yes, that's a off-by-one bug, add 1+ at the end of uniquify.

--
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://www.jwdt.com/~paysan/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jonah Thomas  
View profile  
 More options May 26 2008, 3:26 pm
Newsgroups: comp.lang.forth
From: Jonah Thomas <jethom...@gmail.com>
Date: Mon, 26 May 2008 15:26:22 -0400
Local: Mon, May 26 2008 3:26 pm
Subject: Re: Elementary but surprisingly difficult.
Albert van der Horst <alb...@spenarnc.xs4all.nl> wrote:

> Sorting the items is pretty easy, qsort can handle that.
> Now eliminating the duplicates, we need only compare one
> item to the next.
> Something along compact (start, end, len -- end' )
> The new end should be returned, of course.

> Even using variables/locals, I find this difficult.
> (More difficult then e.g. making a table for the number of
> dividers of N, which sounds much less trivial.)

There are lots of sort algorithms available. If you find the right one, you can delete duplicates when the sort notices them. Then you don't have to sort them.

I started to do that. I found a simple comb-sort that was supposed to be pretty fast, and I looked at how to make simple modifications so it would close up duplicates instead of sorting them. Then I checked back here and found that you no longer wanted the routine at all. Oh well.

I'm not sure it would have been worth doing. For a long sequence you might have a long block move for every individual duplicate. The simplest way it would be a block move on average half as long as the whole sequence. Easy to add a little complication and make it a block move on average a quarter as long as the whole sequence. Maybe a different sort would work better.

Here's the combsort, in case anybody has any interest.

-------
\ Combsort
\ Adapted from code by Wayne Conrad and Nick Estes
\ http://yagni.com/combsort/forth/index.php

defer less

: newgap ( gap -- gap )
   10 13 */
   dup 9 11 within if
     drop 11
   then 1 max ;

: swap-by-pointer? ( p1 p2 -- flag )
   >r dup @ r@ @ less if
     r@ @ over @ r> ! swap ! -1 else
     r> 2drop 0 then ;

: comb ( gap 0 array length -- flag )
   bounds ?do
     over i + i swap-by-pointer? or
   1 cells +loop nip ;

: combsort ( array length -- )
   dup
   begin
     newgap dup >r cells
     0 2over r@ - cells comb
     r@ swap 0= r> 2 < and
   until
   drop 2drop ;
-----


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Anton Ertl  
View profile  
 More options May 26 2008, 3:29 pm
Newsgroups: comp.lang.forth
From: an...@mips.complang.tuwien.ac.at (Anton Ertl)
Date: Mon, 26 May 2008 19:29:33 GMT
Local: Mon, May 26 2008 3:29 pm
Subject: Re: Elementary but surprisingly difficult.

Bernd Paysan <bernd.pay...@gmx.de> writes:
>Albert van der Horst wrote:
>> The main problem is that the content cannot be placed
>> on the stack they cannot be compared with <> etc.

>I don't see the big problem. If you can't place the content on the stack,
>place a pointer. And compare with whatever compare function you need. If
>you want this a library function like qsort, define that your array is an
>array of pointers to the actual structures, and that you have a deferred
>word for the actual comparison.

No!  Don't use a global variable (like a deferred word) for that; it's
not reentrant.  Instead, pass the xt of the comparison word on the
stack, or pass the right comparison word with the data (e.g., by using
an OO extension).

- anton
--
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2008: http://www.euroforth.org/ef08.html


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bill Spight  
View profile  
 More options May 30 2008, 9:57 am
Newsgroups: comp.lang.forth
From: Bill Spight <bXspi...@aol.mac>
Date: Fri, 30 May 2008 13:57:12 GMT
Local: Fri, May 30 2008 9:57 am
Subject: Re: Elementary but surprisingly difficult.

On Sun, 25 May 2008 01:54:39 -0400, John Passaniti wrote:
> Albert van der Horst wrote:
>> Sorting the items is pretty easy, qsort can handle that.
>> Now eliminating the duplicates, we need only compare one
>> item to the next.

> What if the original ordering must be preserved?  By sorting, you've
> destroyed that ordering.  

Errr. "abcbdecadec" Eliminate duplicates but preserve the original order?
Impossible.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Passaniti  
View profile  
 More options May 31 2008, 11:58 pm
Newsgroups: comp.lang.forth
From: John Passaniti <n...@JapanIsShinto.com>
Date: Sat, 31 May 2008 23:58:42 -0400
Local: Sat, May 31 2008 11:58 pm
Subject: Re: Elementary but surprisingly difficult.

Bill Spight wrote:
>> What if the original ordering must be preserved?  By sorting, you've
>> destroyed that ordering.  

> Errr. "abcbdecadec" Eliminate duplicates but preserve the original order?
> Impossible.

"abcde"

I apparently just did the impossible.

Here, allow me to use a different string:

"edecdbea"

Using my awesome powers of removing duplicates without sorting and
without changing the order as I progress through the string, I get:

"edcba"

Later, Albert van der Horst restated the problem not as removing
duplicates from an array, but removing duplicates from a set.  As a set
problem, order doesn't matter.  But still, I continue to wonder why this
discussion involves any sorting.  It doesn't make the problem any easier
and it invariably requires pointless memory moves of either the contents
of the array or a set of addresses to that contents.  My solution
required only a single pass through the array and the creation of a
parallel data structure to mark values that were previously seen.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Marcel Hendrix  
View profile  
 More options Jun 1 2008, 2:41 am
Newsgroups: comp.lang.forth
From: m...@iae.nl (Marcel Hendrix)
Date: Sun, 1 Jun 2008 08:41:13 +0200
Local: Sun, Jun 1 2008 2:41 am
Subject: Re: Elementary but surprisingly difficult.
John Passaniti <n...@JapanIsShinto.com> wrote Re: Elementary but surprisingly difficult.

> Bill Spight wrote:
>>> What if the original ordering must be preserved?  By sorting, you've
>>> destroyed that ordering.  
>> Errr. "abcbdecadec" Eliminate duplicates but preserve the original order?
>> Impossible.
> "abcde"
> I apparently just did the impossible.

"badec"

[..]

-marcel


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 49   Newer >
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google