Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #34847 > unrolled thread
| Started by | hughaguilar96@gmail.com |
|---|---|
| First post | 2014-12-08 16:43 -0800 |
| Last post | 2014-12-14 11:58 +0000 |
| Articles | 20 — 7 participants |
Back to article view | Back to comp.lang.forth
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2014-12-08 16:43 -0800
Re: Rosetta Code: Top rank per group Melzzzzz <mel@zzzzz.com> - 2014-12-09 05:08 +0100
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2014-12-09 21:43 -0800
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2014-12-09 21:55 -0800
Re: Rosetta Code: Top rank per group november.nihal@gmail.com - 2014-12-10 03:40 -0800
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2014-12-11 19:15 -0800
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2014-12-13 09:11 -0800
Re: Rosetta Code: Top rank per group "WJ" <w_a_x_man@yahoo.com> - 2014-12-15 06:08 +0000
Re: Rosetta Code: Top rank per group "WJ" <w_a_x_man@yahoo.com> - 2014-12-17 02:50 +0000
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2014-12-18 22:06 -0800
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2014-12-21 17:14 -0800
Re: Rosetta Code: Top rank per group Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2014-12-22 10:27 +0000
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2014-12-22 22:03 -0800
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2015-01-08 17:17 -0800
Re: Rosetta Code: Top rank per group Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2015-01-09 09:56 +0000
Re: Rosetta Code: Top rank per group hughaguilar96@gmail.com - 2015-03-16 19:11 -0700
Re: Rosetta Code: Top rank per group "WJ" <w_a_x_man@yahoo.com> - 2014-12-24 05:25 +0000
Re: Rosetta Code: Top rank per group mix <mix@test.net> - 2014-12-14 11:55 +0000
Re: Rosetta Code: Top rank per group empty-buffers <empty.buffersYOU-DONT-WANT-THIS@gmail.NEITHER-THIS.com> - 2014-12-14 13:52 +0000
Re: Rosetta Code: Top rank per group mix <mix@test.net> - 2014-12-14 11:58 +0000
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2014-12-08 16:43 -0800 |
| Subject | Re: Rosetta Code: Top rank per group |
| Message-ID | <f7ed596e-a72d-4e9f-bcc7-82fc6037498f@googlegroups.com> |
On Wednesday, February 12, 2014 6:37:15 PM UTC-7, Julian Fondren wrote: > 1. brute-force the answer with high-level data structures: walk the table, > adding departments as keys to a hash table, the values of which are lists of > salaries. Once you've exhausted the table, sort the keys of the hash (for nice > output) and then print them and their top N of the sorted lists of salaries. > > (If you've never used a Perl oneliner that you wrote in a minute to chew on > multiple repquota streams and spit out the top diskspace-using users, then that > I call this a 'good solution' may puzzle you.) A program like this is trivial using my ASSOCIATION.4TH in the novice package: http://www.forth.org/novice.html It uses an LLRB tree internally, and it allows associations (or subsets of associations) to be traversed in order. I would just build one association sorted by department, then for each department I would build an association sorted by salary, then just dump out the leftmost N nodes. For a real program that has a lot of data and has to be robust, it would make more sense to build an association sorted by name, and then build associations sorted by department, salary, etc. in which the record is a pointer to a node in the main association --- like a relational-database, this saves memory by not replicating data. Note that I have SPLIT that converts comma-deliminated strings into lists of fields. This works for any deliminator, so it can first break up the comma-deliminated strings into a list, and then break up the name field into a sublist of first-name and last-name. Also, it allows text to be in quotes, so everything in the quotes will be considered a field even if it contains the delimiter. I don't think Rosetta Stone allows the use of libraries however --- so it favors languages such as Perl that have data-structures built in --- if the programmer has to implement all of these data-structures from scratch as Elizabeth Rather demands her cult members to do, the program is going to be huge and complicated. It will also likely use an inefficient data-structure such as a basic binary-tree rather than an LLRB tree, because implementing an LLRB tree for a single program is way too much work. Actually, implementing a basic binary tree with insert and delete is a lot of work too, if it has to be done from scratch for every program. This is why ANS-Forth is never used in commercial programming --- implementing everything from scratch is just too much work --- the boss expects a trivial program like this to be done in an hour or two, and he will fire a programmer who spends a week on it and then says: "Well, um, I'm bogged down in figuring out how LLRB trees work, as they are quite complicated!"
[toc] | [next] | [standalone]
| From | Melzzzzz <mel@zzzzz.com> |
|---|---|
| Date | 2014-12-09 05:08 +0100 |
| Message-ID | <20141209050842.1695200d@maxa-pc> |
| In reply to | #34847 |
On Mon, 8 Dec 2014 16:43:22 -0800 (PST) hughaguilar96@gmail.com wrote: > On Wednesday, February 12, 2014 6:37:15 PM UTC-7, Julian Fondren > wrote: > > 1. brute-force the answer with high-level data structures: walk the > > table, adding departments as keys to a hash table, the values of > > which are lists of salaries. Once you've exhausted the table, sort > > the keys of the hash (for nice output) and then print them and > > their top N of the sorted lists of salaries. > > > > (If you've never used a Perl oneliner that you wrote in a minute to > > chew on multiple repquota streams and spit out the top > > diskspace-using users, then that I call this a 'good solution' may > > puzzle you.) > > A program like this is trivial using my ASSOCIATION.4TH in the novice > package: http://www.forth.org/novice.html > It uses an LLRB tree internally, and it allows associations (or > subsets of associations) to be traversed in order. I would just build > one association sorted by department, then for each department I > would build an association sorted by salary, then just dump out the > leftmost N nodes. For a real program that has a lot of data and has > to be robust, it would make more sense to build an association sorted > by name, and then build associations sorted by department, salary, > etc. in which the record is a pointer to a node in the main > association --- like a relational-database, this saves memory by not > replicating data. Hm, sounds complicated. SQL/Database is best for such tasks... You just have to send SQL command and that's all. Not worth a fuss... > > I don't think Rosetta Stone allows the use of libraries however --- > so it favors languages such as Perl that have data-structures built > in --- if the programmer has to implement all of these > data-structures from scratch as Elizabeth Rather demands her cult > members to do, the program is going to be huge and complicated. It > will also likely use an inefficient data-structure such as a basic > binary-tree rather than an LLRB tree, because implementing an LLRB > tree for a single program is way too much work. What's so good about LLRB tree? I think that classical RB tree performs better. Only advantage of LLRB is like AA tree ease of implementation, I guess... > > Actually, implementing a basic binary tree with insert and delete is > a lot of work too, Nope, not much of a code. It requires some thinking but nothing one cannot finish in couple of days. if it has to be done from scratch for every > program. One can write it once and use it in every other program. It is not necessary to write everytime one starts new program. Problem is that like C (which also doesn;t have standard container library) Forth is low level language and people can't agree on interface. This is why ANS-Forth is never used in commercial > programming --- implementing everything from scratch is just too much > work --- the boss expects a trivial program like this to be done in > an hour or two, and he will fire a programmer who spends a week on it > and then says: "Well, um, I'm bogged down in figuring out how LLRB > trees work, as they are quite complicated!" There is internet, but then again, there are programmers that make memory leaks in Java :p
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2014-12-09 21:43 -0800 |
| Message-ID | <5db66927-a0c1-44e9-9d4c-8d2ec4b6bb38@googlegroups.com> |
| In reply to | #34851 |
On Monday, December 8, 2014 9:08:43 PM UTC-7, Melzzzzz wrote: > On Mon, 8 Dec 2014 16:43:22 -0800 (PST) > hughaguilar96@gmail.com wrote: > > A program like this is trivial using my ASSOCIATION.4TH in the novice > > package: http://www.forth.org/novice.html > > It uses an LLRB tree internally, and it allows associations (or > > subsets of associations) to be traversed in order. I would just build > > one association sorted by department, then for each department I > > would build an association sorted by salary, then just dump out the > > leftmost N nodes. For a real program that has a lot of data and has > > to be robust, it would make more sense to build an association sorted > > by name, and then build associations sorted by department, salary, > > etc. in which the record is a pointer to a node in the main > > association --- like a relational-database, this saves memory by not > > replicating data. > > Hm, sounds complicated. SQL/Database is best for such tasks... > You just have to send SQL command and that's all. Not worth a fuss... Using a database is going to be hugely slow --- I wouldn't do this unless there was so much data that it wouldn't fit in memory all at once. If using SQL is allowed by Rosetta Stone however, then I suppose they will allow me to use my novice package. It is not clear to me what the purpose of Rosetta Stone is --- we are comparing languages' libraries? > > I don't think Rosetta Stone allows the use of libraries however --- > > so it favors languages such as Perl that have data-structures built > > in --- if the programmer has to implement all of these > > data-structures from scratch as Elizabeth Rather demands her cult > > members to do, the program is going to be huge and complicated. It > > will also likely use an inefficient data-structure such as a basic > > binary-tree rather than an LLRB tree, because implementing an LLRB > > tree for a single program is way too much work. > > What's so good about LLRB tree? I think that classical RB tree > performs better. Only advantage of LLRB is like AA tree ease > of implementation, I guess... Have you actually implemented an RB tree? This stuff is more complicated than you might suppose --- I would use the term "ease of implementation" for LLRB trees. > > Actually, implementing a basic binary tree with insert and delete is > > a lot of work too, > > Nope, not much of a code. It requires some thinking but nothing > one cannot finish in couple of days. True, but the whole program should be completed in one or two hours. > if it has to be done from scratch for every > > program. > > One can write it once and use it in every other program. > It is not necessary to write everytime one starts new > program. Problem is that like C (which also doesn;t have > standard container library) Forth is low level language > and people can't agree on interface. The problem with C is not that people can't agree on the interface --- the problem is that its type system gets in the way of passing a pointer to a function as a parameter --- the whole point of the exercise is that you can have various functions each working with different data types, but if the data-type has to be declared in the declaration for the function, then that defeats your purpose! The type system can be defeated using VOID declarations, but the result is an incredibly ugly mess. Forth doesn't have the problem of type declarations, so it much easier in Forth. The problem in Forth is just basic ignorance: On Thursday, September 26, 2013 12:09:59 PM UTC-7, Elizabeth D. Rather wrote: > On 9/26/13 2:27 AM, Albert van der Horst wrote: > > In article <043a32ac-c009-4aff-beda-aa4041daadcb@googlegroups.com>, > > <hughag...@yahoo.com> wrote: > >> On Wednesday, September 25, 2013 11:44:35 AM UTC-7, Elizabeth D. Rather wrote: > >>> But my point is that you can define data objects to behave *exactly* the > >>> way you want them to, to solve the problem at hand. > > >> I totally disagree with this. It is foolish to custom-implement basic > >> data structures such as arrays for every application. It is easy to > >> write a general-purpose array or linked-list or whatever, that works > >> exactly the way that you want it to for *every* application. > Does "*every* application" you write use exactly the same kind of data > arranged in the same way? If so, having written it once you can reuse it > often. If not, you either have to rearrange your data to make it work or > modify your "general-purpose" structure. ANS-Forth has become a cult of ignorance. Everybody who programs in ANS-Forth feels required to maintain a level of ignorance equal to Elizabeth Rather's so that she will allow them to be members of the cult. Because Elizabeth Rather doesn't understand the concept of general-purpose data-structures, all ANS-Forth programmers are required to also be ignorant of the subject --- write every program entirely from scratch as she requires --- this is why ANS-Forth will always be considered a toy language. > This is why ANS-Forth is never used in commercial > > programming --- implementing everything from scratch is just too much > > work --- the boss expects a trivial program like this to be done in > > an hour or two, and he will fire a programmer who spends a week on it > > and then says: "Well, um, I'm bogged down in figuring out how LLRB > > trees work, as they are quite complicated!" > > There is internet, but then again, there are programmers that make > memory leaks in Java :p My experience with implementing the LLRB tree, is that there is nothing directly useful on the internet. Robert Sedgewick invented the LLRB tree. When you read his PDFs however, they provide non-working Java code for the implementation. It is not possible to just port this code over to Forth or any other language; it doesn't even compile because it references functions that aren't provided --- it is Java-like pseudo-code at best. I think Sedgewick purposely crippled this code so that students wouldn't be able to use his code without understanding the algorithm --- prevent the script kiddies from getting an 'A'. I had to actually figure out the algorithm to implement it, which wasn't easy (for me; maybe easier for others). There are implementations that actually work (more than one for CLOS), so if you assume that whoever wrote those knew what he was doing, then you can port those directly into Forth or whatever without understanding the algorithm. I didn't do this, because I don't have so much faith in other people's understanding of subjects that I don't understand --- so I did it the hard way, and figured out the algorithm (but now I've forgotten it!).
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2014-12-09 21:55 -0800 |
| Message-ID | <259bfc01-e1dd-41a4-9760-bde22e19890c@googlegroups.com> |
| In reply to | #34856 |
On Monday, December 8, 2014 9:08:43 PM UTC-7, Melzzzzz wrote: > On Mon, 8 Dec 2014 16:43:22 -0800 (PST) > hughaguilar96@gmail.com wrote: > > A program like this is trivial using my ASSOCIATION.4TH in the novice > > package: http://www.forth.org/novice.html > > It uses an LLRB tree internally, and it allows associations (or > > subsets of associations) to be traversed in order. I would just build > > one association sorted by department, then for each department I > > would build an association sorted by salary, then just dump out the > > leftmost N nodes. For a real program that has a lot of data and has > > to be robust, it would make more sense to build an association sorted > > by name, and then build associations sorted by department, salary, > > etc. in which the record is a pointer to a node in the main > > association --- like a relational-database, this saves memory by not > > replicating data. > > Hm, sounds complicated. SQL/Database is best for such tasks... > You just have to send SQL command and that's all. Not worth a fuss... Using a database is going to be hugely slow --- I wouldn't do this unless there was so much data that it wouldn't fit in memory all at once. If using SQL is allowed by Rosetta Stone however, then I suppose they will allow me to use my novice package. It is not clear to me what the purpose of Rosetta Stone is --- we are comparing languages' libraries? > > I don't think Rosetta Stone allows the use of libraries however --- > > so it favors languages such as Perl that have data-structures built > > in --- if the programmer has to implement all of these > > data-structures from scratch as Elizabeth Rather demands her cult > > members to do, the program is going to be huge and complicated. It > > will also likely use an inefficient data-structure such as a basic > > binary-tree rather than an LLRB tree, because implementing an LLRB > > tree for a single program is way too much work. > > What's so good about LLRB tree? I think that classical RB tree > performs better. Only advantage of LLRB is like AA tree ease > of implementation, I guess... Have you actually implemented an RB tree? This stuff is more complicated than you might suppose --- I would not use the term "ease of implementation" for LLRB trees. > > Actually, implementing a basic binary tree with insert and delete is > > a lot of work too, > > Nope, not much of a code. It requires some thinking but nothing > one cannot finish in couple of days. True, but the whole program should be completed in one or two hours. > if it has to be done from scratch for every > > program. > > One can write it once and use it in every other program. > It is not necessary to write everytime one starts new > program. Problem is that like C (which also doesn;t have > standard container library) Forth is low level language > and people can't agree on interface. The problem with C is not that people can't agree on the interface --- the problem is that its type system gets in the way of passing a pointer to a function as a parameter --- the whole point of the exercise is that you can have various functions each working with different data types, but if the data-type has to be declared in the declaration for the function, then that defeats your purpose! The type system can be defeated using VOID declarations, but the result is an incredibly ugly mess. Forth doesn't have the problem of type declarations, so it much easier in Forth. The problem in Forth is just basic ignorance: On Thursday, September 26, 2013 12:09:59 PM UTC-7, Elizabeth D. Rather wrote: > On 9/26/13 2:27 AM, Albert van der Horst wrote: > > In article <043a32ac-c009-4aff-beda-aa4041daadcb@googlegroups.com>, > > <hughag...@yahoo.com> wrote: > >> On Wednesday, September 25, 2013 11:44:35 AM UTC-7, Elizabeth D. Rather wrote: > >>> But my point is that you can define data objects to behave *exactly* the > >>> way you want them to, to solve the problem at hand. > > >> I totally disagree with this. It is foolish to custom-implement basic > >> data structures such as arrays for every application. It is easy to > >> write a general-purpose array or linked-list or whatever, that works > >> exactly the way that you want it to for *every* application. > Does "*every* application" you write use exactly the same kind of data > arranged in the same way? If so, having written it once you can reuse it > often. If not, you either have to rearrange your data to make it work or > modify your "general-purpose" structure. ANS-Forth has become a cult of ignorance. Everybody who programs in ANS-Forth feels required to maintain a level of ignorance equal to Elizabeth Rather's so that she will allow them to be members of the cult. Because Elizabeth Rather doesn't understand the concept of general-purpose data-structures, all ANS-Forth programmers are required to also be ignorant of the subject --- write every program entirely from scratch as she requires --- this is why ANS-Forth will always be considered a toy language. > This is why ANS-Forth is never used in commercial > > programming --- implementing everything from scratch is just too much > > work --- the boss expects a trivial program like this to be done in > > an hour or two, and he will fire a programmer who spends a week on it > > and then says: "Well, um, I'm bogged down in figuring out how LLRB > > trees work, as they are quite complicated!" > > There is internet, but then again, there are programmers that make > memory leaks in Java :p My experience with implementing the LLRB tree, is that there is nothing directly useful on the internet. Robert Sedgewick invented the LLRB tree. When you read his PDFs however, they provide non-working Java code for the implementation. It is not possible to just port this code over to Forth or any other language; it doesn't even compile because it references functions that aren't provided --- it is Java-like pseudo-code at best. I think Sedgewick purposely crippled this code so that students wouldn't be able to use his code without understanding the algorithm --- prevent the script kiddies from getting an 'A'. I had to actually figure out the algorithm to implement it, which wasn't easy (for me; maybe easier for others). There are implementations that actually work (more than one for CLOS), so if you assume that whoever wrote those knew what he was doing, then you can port those directly into Forth or whatever without understanding the algorithm. I didn't do this, because I don't have so much faith in other people's understanding of subjects that I don't understand --- so I did it the hard way, and figured out the algorithm (but now I've forgotten it!).
[toc] | [prev] | [next] | [standalone]
| From | november.nihal@gmail.com |
|---|---|
| Date | 2014-12-10 03:40 -0800 |
| Message-ID | <b0bd83eb-8a4e-47ba-afec-9d0ca958c6ec@googlegroups.com> |
| In reply to | #34857 |
Q : It is not clear to me what the purpose of Rosetta Stone is... The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different. Whilst your novice is not part of every forth, its ans-forth, its free and freely available. I don't think there is anything that forbids the use of including libs/modules/files although "I think" these are meant to be standard, so that the solutions can be tried by as many coders as possible and also to avoid messages such as " I cant run this on my machine".
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2014-12-11 19:15 -0800 |
| Message-ID | <55598639-8854-48a6-afa5-797616f98aac@googlegroups.com> |
| In reply to | #34859 |
On Wednesday, December 10, 2014 4:40:53 AM UTC-7, novembe...@gmail.com wrote: > Q : It is not clear to me what the purpose of Rosetta Stone is... > > The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different. > > Whilst your novice is not part of every forth, its ans-forth, its free and freely available. > > I don't think there is anything that forbids the use of including libs/modules/files although "I think" these are meant to be standard, so that the solutions can be tried by as many coders as possible and also to avoid messages such as " I cant run this on my machine". Well, I will write it this weekend when I have time. I looked at the C solution, and they sort both the department and the salary at the same time --- that is simpler than what I suggested, in which I sort by department first, and then sort by salary second to make sub-associations --- I suppose I should do that too. I think these exercises are too contrived. In a real program, you want to hold your data in a data-structure that is robust, so you can do anything that you want with the data as need be. An array isn't a good choice because it doesn't allow for insertion and deletion very well. My tree is much more robust, but this doesn't come into play given a single query as we have in this exercise.
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2014-12-13 09:11 -0800 |
| Message-ID | <9ddade33-8b5a-4eab-8191-fc7f2398f0bc@googlegroups.com> |
| In reply to | #34878 |
On Thursday, December 11, 2014 8:15:36 PM UTC-7, hughag...@gmail.com wrote:
> On Wednesday, December 10, 2014 4:40:53 AM UTC-7, novembe...@gmail.com wrote:
> > Q : It is not clear to me what the purpose of Rosetta Stone is...
> >
> > The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different.
> >
> > Whilst your novice is not part of every forth, its ans-forth, its free and freely available.
> >
> > I don't think there is anything that forbids the use of including libs/modules/files although "I think" these are meant to be standard, so that the solutions can be tried by as many coders as possible and also to avoid messages such as " I cant run this on my machine".
>
> Well, I will write it this weekend when I have time.
>
> I looked at the C solution, and they sort both the department and the salary at the same time --- that is simpler than what I suggested, in which I sort by department first, and then sort by salary second to make sub-associations --- I suppose I should do that too.
>
> I think these exercises are too contrived. In a real program, you want to hold your data in a data-structure that is robust, so you can do anything that you want with the data as need be. An array isn't a good choice because it doesn't allow for insertion and deletion very well. My tree is much more robust, but this doesn't come into play given a single query as we have in this exercise.
Well, I wrote it last night. I used LIST.4TH rather than ASSOCIATION.4TH because there is less rigamarole involved --- the trees need handles, whereas the lists don't (with the lists, I just work with a pointer to the head of the list). ASSOCIATION.4TH is my "big gun," and I don't use that unless I actually need its features (such as traversing both forward and backward) --- for such a trivial program as this, LIST.4TH is more than adequate.
Notice that I don't use iteration at all; there are no DO loops, BEGIN loops, or any other kind of loop, and no recursion either. Looking over the examples from other languages, I would categorize the languages into three groups:
1.) high-level languages --- These languages allow programs to routinely be written without explicit iteration. An example is Factor. This is a good thing, because explicit iteration is a major source of bugs (off-by-one, boundary, etc.), so if you don't have explicit iteration then your functions just do one thing and are easy to write and easy to test (my program had one small bug that I quickly fixed, and then it worked perfectly, and most of my code works the first time it is tested).
2.) medium-level languages --- These languages allow programs to be written without explicit iteration, but it is a major hassle to do this and hence it is rarely done. An example is C. Many C programmers don't know how to do this, despite using code such as QSORT that does do this.
3.) low-level languages --- These languages don't allow programs to be written without explicit iteration. COBOL is an example of this. Elizabeth Rather's background is in COBOL, which is why she doesn't understand how programs can be written without explicit iteration --- she has spent her entire life trying to make Forth into the COBOL of micro-controllers.
Anyway, here is the program. I removed most of the comments because the lines are too long and they wrap around, making a mess. If anybody wants the version with comments, send me an email asking for this, and I will send it. Also, this program will become yet another example program in the novice package in the next release.
\ "Top Rank per Group" by Hugh Aguilar
\ Requires the novice package: http://www.forth.org/novice.html
\ The following should already be done:
\ include novice.4th
\ include list.4th
marker TopRank.4th
\ ******
\ ****** The following defines our data-structure.
\ ****** Pretty much every struct definition has these basic functions.
\ ******
list
w field .name \ string
w field .id \ string
w field .salary \ integer
w field .dept \ string
constant employee
: init-employee ( name id salary department node -- node )
init-list >r
hstr r@ .dept !
r@ .salary !
hstr r@ .id !
hstr r@ .name !
r> ;
: new-employee ( name id salary dept -- node )
employee alloc
init-employee ;
: <kill-employee> ( node -- )
dup .name @ dealloc
dup .id @ dealloc
dup .dept @ dealloc
dealloc ;
: kill-employee ( head -- )
each[ <kill-employee> ]each ;
: <clone-employee> ( node -- new-node )
clone-node
dup .name @ hstr over .name !
dup .id @ hstr over .id !
dup .dept @ hstr over .dept ! ;
: clone-employee ( head -- new-head )
nil
swap each[ <clone-employee> link ]each ;
: <show-employee> ( node -- )
dup .id @ count type 4 spaces
dup .dept @ count type 4 spaces
dup .salary @ . 4 spaces
dup .name @ count type cr
drop ;
: show-employee ( head -- )
cr
each[ <show-employee> ]each ;
\ ******
\ ****** The following code is specific to the query that we want to do.
\ ******
: employee-dept-salary ( new-node node -- new-node ? )
2dup
.dept @ count rot .dept @ count compare ?dup if A>B = nip exit then
.salary @ over .salary @ < ;
: <top> ( n rank current-dept head node -- n rank current-dept head )
2>r \ -- n rank current-dept
dup count r@ .dept @ count compare A=B <> if
2drop
0 r@ .dept @ then
rover rover > if
r> r> over \ -- n rank current-dept node head node
<clone-employee> link \ -- n rank current-dept node head
>r >r then \ -- n rank current-dept
swap 1+ swap
rdrop r> ; \ -- n rank current-dept head
: top ( n head -- new-head )
>r
0 c" xxx" nil \ -- n rank current-dept new-head
r> ['] <top> each
3nip ;
\ ******
\ ****** The following is a test of the program using sample data.
\ ******
nil ' employee-dept-salary
c" Tyler Bennett" c" E10297" 32000 c" D101" new-employee insert-ordered
c" John Rappl" c" E21437" 47000 c" D050" new-employee insert-ordered
c" George Woltman" c" E00127" 53500 c" D101" new-employee insert-ordered
c" Adam Smith" c" E63535" 18000 c" D202" new-employee insert-ordered
c" Claire Buckman" c" E39876" 27800 c" D202" new-employee insert-ordered
c" David McClellan" c" E04242" 41500 c" D101" new-employee insert-ordered
c" Rich Holcomb" c" E01234" 49500 c" D202" new-employee insert-ordered
c" Nathan Adams" c" E41298" 21900 c" D050" new-employee insert-ordered
c" Richard Potter" c" E43128" 15900 c" D101" new-employee insert-ordered
c" David Motsinger" c" E27002" 19250 c" D202" new-employee insert-ordered
c" Tim Sampair" c" E03033" 27000 c" D101" new-employee insert-ordered
c" Kim Arlich" c" E10001" 57000 c" D190" new-employee insert-ordered
c" Timothy Grove" c" E16398" 29900 c" D190" new-employee insert-ordered
drop constant test-data
cr .( N = 0 )
0 test-data top dup show-employee kill-employee
cr .( N = 1 )
1 test-data top dup show-employee kill-employee
cr .( N = 2 )
2 test-data top dup show-employee kill-employee
cr .( N = 3 )
3 test-data top dup show-employee kill-employee
test-data kill-employee
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2014-12-15 06:08 +0000 |
| Message-ID | <m6ltt3$qca$1@dont-email.me> |
| In reply to | #34900 |
hughaguilar96@gmail.com wrote:
> \ "Top Rank per Group" by Hugh Aguilar
> \ Requires the novice package: http://www.forth.org/novice.html
> \ The following should already be done:
> \ include novice.4th
> \ include list.4th
>
> marker TopRank.4th
>
> \ ******
> \ ****** The following defines our data-structure.
> \ ****** Pretty much every struct definition has these basic functions.
> \ ******
>
> list
> w field .name \ string
> w field .id \ string
> w field .salary \ integer
> w field .dept \ string
> constant employee
>
> : init-employee ( name id salary department node -- node )
> init-list >r
> hstr r@ .dept !
> r@ .salary !
> hstr r@ .id !
> hstr r@ .name !
> r> ;
>
> : new-employee ( name id salary dept -- node )
> employee alloc
> init-employee ;
>
> : <kill-employee> ( node -- )
> dup .name @ dealloc
> dup .id @ dealloc
> dup .dept @ dealloc
> dealloc ;
>
> : kill-employee ( head -- )
> each[ <kill-employee> ]each ;
>
> : <clone-employee> ( node -- new-node )
> clone-node
> dup .name @ hstr over .name !
> dup .id @ hstr over .id !
> dup .dept @ hstr over .dept ! ;
>
> : clone-employee ( head -- new-head )
> nil
> swap each[ <clone-employee> link ]each ;
>
> : <show-employee> ( node -- )
> dup .id @ count type 4 spaces
> dup .dept @ count type 4 spaces
> dup .salary @ . 4 spaces
> dup .name @ count type cr
> drop ;
>
> : show-employee ( head -- )
> cr
> each[ <show-employee> ]each ;
>
>
> \ ******
> \ ****** The following code is specific to the query that we want to do.
> \ ******
>
> : employee-dept-salary ( new-node node -- new-node ? )
> 2dup
> .dept @ count rot .dept @ count compare ?dup if A>B = nip exit then
> .salary @ over .salary @ < ;
>
> : <top> ( n rank current-dept head node -- n rank current-dept head )
> 2>r \ -- n rank current-dept
> dup count r@ .dept @ count compare A=B <> if
> 2drop
> 0 r@ .dept @ then
> rover rover > if
> r> r> over \ -- n rank current-dept node head node
> <clone-employee> link \ -- n rank current-dept node head
> >r >r then \ -- n rank current-dept
> swap 1+ swap
> rdrop r> ; \ -- n rank current-dept head
>
> : top ( n head -- new-head )
> >r
> 0 c" xxx" nil \ -- n rank current-dept new-head
> r> ['] <top> each
> 3nip ;
>
>
> \ ******
> \ ****** The following is a test of the program using sample data.
> \ ******
>
> nil ' employee-dept-salary
> c" Tyler Bennett" c" E10297" 32000 c" D101" new-employee insert-ordered
> c" John Rappl" c" E21437" 47000 c" D050" new-employee insert-ordered
> c" George Woltman" c" E00127" 53500 c" D101" new-employee insert-ordered
> c" Adam Smith" c" E63535" 18000 c" D202" new-employee insert-ordered
> c" Claire Buckman" c" E39876" 27800 c" D202" new-employee insert-ordered
> c" David McClellan" c" E04242" 41500 c" D101" new-employee insert-ordered
> c" Rich Holcomb" c" E01234" 49500 c" D202" new-employee insert-ordered
> c" Nathan Adams" c" E41298" 21900 c" D050" new-employee insert-ordered
> c" Richard Potter" c" E43128" 15900 c" D101" new-employee insert-ordered
> c" David Motsinger" c" E27002" 19250 c" D202" new-employee insert-ordered
> c" Tim Sampair" c" E03033" 27000 c" D101" new-employee insert-ordered
> c" Kim Arlich" c" E10001" 57000 c" D190" new-employee insert-ordered
> c" Timothy Grove" c" E16398" 29900 c" D190" new-employee insert-ordered
> drop constant test-data
It was too difficult to get the data from a string or from a file.
>
> cr .( N = 0 )
> 0 test-data top dup show-employee kill-employee
>
> cr .( N = 1 )
> 1 test-data top dup show-employee kill-employee
>
> cr .( N = 2 )
> 2 test-data top dup show-employee kill-employee
>
> cr .( N = 3 )
> 3 test-data top dup show-employee kill-employee
>
> test-data kill-employee
Ruby:
Person = Struct.new( :name, :id, :salary, :dept )
str = "
Tyler Bennett,E10297,32000,D101
John Rappl,E21437,47000,D050
George Woltman,E00127,53500,D101
Adam Smith,E63535,18000,D202
Claire Buckman,E39876,27800,D202
David McClellan,E04242,41500,D101
Rich Holcomb,E01234,49500,D202
Nathan Adams,E41298,21900,D050
Richard Potter,E43128,15900,D101
David Motsinger,E27002,19250,D202
Tim Sampair,E03033,27000,D101
Kim Arlich,E10001,57000,D190
Timothy Grove,E16398,29900,D190"
records = str.strip.split( /\s*\n\s*/ ).
map{|s| a,b,c,d = s.split(","); Person.new(a, b, c.to_i, d)}
def print_rec( record, col_widths)
print " "
record.to_a.zip( col_widths ).
each{|x,w|
if x.is_a? String then
print x.ljust(w)
else
print x.to_s.rjust(w)
end
print " "}
puts
end
def top_salaries( n, data )
column_widths = data.map( &:to_a ).transpose.
map{|column| column.map{|x| x.to_s.size}.max }
departments = data.map( &:dept ).uniq.sort
departments.each{|dept|
puts dept
data.select{|rec| dept == rec.dept}.
sort_by{|rec| -rec.salary}[0,n].
each{|rec| print_rec rec, column_widths}
}
end
top_salaries(0,records)
D050
D101
D190
D202
top_salaries(2,records)
D050
John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
D101
George Woltman E00127 53500 D101
David McClellan E04242 41500 D101
D190
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190
D202
Rich Holcomb E01234 49500 D202
Claire Buckman E39876 27800 D202
top_salaries(999_888_777, records)
D050
John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
D101
George Woltman E00127 53500 D101
David McClellan E04242 41500 D101
Tyler Bennett E10297 32000 D101
Tim Sampair E03033 27000 D101
Richard Potter E43128 15900 D101
D190
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190
D202
Rich Holcomb E01234 49500 D202
Claire Buckman E39876 27800 D202
David Motsinger E27002 19250 D202
Adam Smith E63535 18000 D202
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2014-12-17 02:50 +0000 |
| Message-ID | <m6qr1m$ctc$1@dont-email.me> |
| In reply to | #34900 |
hughaguilar96@gmail.com wrote:
>
> \ "Top Rank per Group" by Hugh Aguilar
> \ Requires the novice package: http://www.forth.org/novice.html
> \ The following should already be done:
> \ include novice.4th
> \ include list.4th
>
> marker TopRank.4th
>
> \ ******
> \ ****** The following defines our data-structure.
> \ ****** Pretty much every struct definition has these basic functions.
> \ ******
>
> list
> w field .name \ string
> w field .id \ string
> w field .salary \ integer
> w field .dept \ string
> constant employee
>
> : init-employee ( name id salary department node -- node )
> init-list >r
> hstr r@ .dept !
> r@ .salary !
> hstr r@ .id !
> hstr r@ .name !
> r> ;
>
> : new-employee ( name id salary dept -- node )
> employee alloc
> init-employee ;
>
> : <kill-employee> ( node -- )
> dup .name @ dealloc
> dup .id @ dealloc
> dup .dept @ dealloc
> dealloc ;
>
> : kill-employee ( head -- )
> each[ <kill-employee> ]each ;
>
> : <clone-employee> ( node -- new-node )
> clone-node
> dup .name @ hstr over .name !
> dup .id @ hstr over .id !
> dup .dept @ hstr over .dept ! ;
>
> : clone-employee ( head -- new-head )
> nil
> swap each[ <clone-employee> link ]each ;
>
> : <show-employee> ( node -- )
> dup .id @ count type 4 spaces
> dup .dept @ count type 4 spaces
> dup .salary @ . 4 spaces
> dup .name @ count type cr
> drop ;
>
> : show-employee ( head -- )
> cr
> each[ <show-employee> ]each ;
>
>
> \ ******
> \ ****** The following code is specific to the query that we want to do.
> \ ******
>
> : employee-dept-salary ( new-node node -- new-node ? )
> 2dup
> .dept @ count rot .dept @ count compare ?dup if A>B = nip exit then
> .salary @ over .salary @ < ;
>
> : <top> ( n rank current-dept head node -- n rank current-dept head )
> 2>r \ -- n rank current-dept
> dup count r@ .dept @ count compare A=B <> if
> 2drop
> 0 r@ .dept @ then
> rover rover > if
> r> r> over \ -- n rank current-dept node head node
> <clone-employee> link \ -- n rank current-dept node head
> >r >r then \ -- n rank current-dept
> swap 1+ swap
> rdrop r> ; \ -- n rank current-dept head
>
> : top ( n head -- new-head )
> >r
> 0 c" xxx" nil \ -- n rank current-dept new-head
> r> ['] <top> each
> 3nip ;
>
>
> \ ******
> \ ****** The following is a test of the program using sample data.
> \ ******
>
> nil ' employee-dept-salary
> c" Tyler Bennett" c" E10297" 32000 c" D101" new-employee insert-ordered
> c" John Rappl" c" E21437" 47000 c" D050" new-employee insert-ordered
> c" George Woltman" c" E00127" 53500 c" D101" new-employee insert-ordered
> c" Adam Smith" c" E63535" 18000 c" D202" new-employee insert-ordered
> c" Claire Buckman" c" E39876" 27800 c" D202" new-employee insert-ordered
> c" David McClellan" c" E04242" 41500 c" D101" new-employee insert-ordered
> c" Rich Holcomb" c" E01234" 49500 c" D202" new-employee insert-ordered
> c" Nathan Adams" c" E41298" 21900 c" D050" new-employee insert-ordered
> c" Richard Potter" c" E43128" 15900 c" D101" new-employee insert-ordered
> c" David Motsinger" c" E27002" 19250 c" D202" new-employee insert-ordered
> c" Tim Sampair" c" E03033" 27000 c" D101" new-employee insert-ordered
> c" Kim Arlich" c" E10001" 57000 c" D190" new-employee insert-ordered
> c" Timothy Grove" c" E16398" 29900 c" D190" new-employee insert-ordered
> drop constant test-data
It was too difficult to extract the data from a string or a file.
>
> cr .( N = 0 )
> 0 test-data top dup show-employee kill-employee
>
> cr .( N = 1 )
> 1 test-data top dup show-employee kill-employee
>
> cr .( N = 2 )
> 2 test-data top dup show-employee kill-employee
>
> cr .( N = 3 )
> 3 test-data top dup show-employee kill-employee
>
> test-data kill-employee
No output?
Gauche Scheme:
;; This will let us treat a list as though it is a structure (record).
(define-record-type (employee (pseudo-rtd <list>)) #t #t
name id salary dept)
(define (get-fields str)
(map (^x (if (#/^\d/ x) (string->number x) x))
(string-split str #\,)))
(define (print-record column-widths record)
(display " ")
(print (string-join
(map
(^(x width)
(if (number? x)
(format "~vD" width x)
(format "~vA" width x)))
record
column-widths))))
(define (get-column-widths records)
(apply
map
(lambda column
(apply max (map (compose string-length x->string) column)))
records))
(define records
(map get-fields
(string-split
"Tyler Bennett,E10297,32000,D101
John Rappl,E21437,47000,D050
George Woltman,E00127,53500,D101
Adam Smith,E63535,18000,D202
Claire Buckman,E39876,27800,D202
David McClellan,E04242,41500,D101
Rich Holcomb,E01234,49500,D202
Nathan Adams,E41298,21900,D050
Richard Potter,E43128,15900,D101
David Motsinger,E27002,19250,D202
Tim Sampair,E03033,27000,D101
Kim Arlich,E10001,57000,D190
Timothy Grove,E16398,29900,D190"
#/\s*\n\s*/)))
(define (top-salaries n records)
(let ((departments (sort (delete-duplicates (map employee-dept records))))
(col-widths (get-column-widths records))
(sorted-by-salary (sort records > employee-salary)))
(dolist (dept departments)
(print dept)
(let1 matches (filter (^x (string=? dept (employee-dept x)))
sorted-by-salary)
(for-each
(pa$ print-record col-widths)
(take* matches n))))))
gosh> (top-salaries 0 records)
D050
D101
D190
D202
gosh> (top-salaries 2 records)
D050
John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
D101
George Woltman E00127 53500 D101
David McClellan E04242 41500 D101
D190
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190
D202
Rich Holcomb E01234 49500 D202
Claire Buckman E39876 27800 D202
gosh> (top-salaries 999000 records)
D050
John Rappl E21437 47000 D050
Nathan Adams E41298 21900 D050
D101
George Woltman E00127 53500 D101
David McClellan E04242 41500 D101
Tyler Bennett E10297 32000 D101
Tim Sampair E03033 27000 D101
Richard Potter E43128 15900 D101
D190
Kim Arlich E10001 57000 D190
Timothy Grove E16398 29900 D190
D202
Rich Holcomb E01234 49500 D202
Claire Buckman E39876 27800 D202
David Motsinger E27002 19250 D202
Adam Smith E63535 18000 D202
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2014-12-18 22:06 -0800 |
| Message-ID | <be4b3f6a-1e54-4f9f-b019-7bbb95fab781@googlegroups.com> |
| In reply to | #34971 |
On Tuesday, December 16, 2014 7:50:55 PM UTC-7, WJ wrote: > hughaguilar96@gmail.com wrote: > > \ ****** > > \ ****** The following is a test of the program using sample data. > > \ ****** > > > > nil ' employee-dept-salary > > c" Tyler Bennett" c" E10297" 32000 c" D101" new-employee insert-ordered > > c" John Rappl" c" E21437" 47000 c" D050" new-employee insert-ordered > > c" George Woltman" c" E00127" 53500 c" D101" new-employee insert-ordered > > c" Adam Smith" c" E63535" 18000 c" D202" new-employee insert-ordered > > c" Claire Buckman" c" E39876" 27800 c" D202" new-employee insert-ordered > > c" David McClellan" c" E04242" 41500 c" D101" new-employee insert-ordered > > c" Rich Holcomb" c" E01234" 49500 c" D202" new-employee insert-ordered > > c" Nathan Adams" c" E41298" 21900 c" D050" new-employee insert-ordered > > c" Richard Potter" c" E43128" 15900 c" D101" new-employee insert-ordered > > c" David Motsinger" c" E27002" 19250 c" D202" new-employee insert-ordered > > c" Tim Sampair" c" E03033" 27000 c" D101" new-employee insert-ordered > > c" Kim Arlich" c" E10001" 57000 c" D190" new-employee insert-ordered > > c" Timothy Grove" c" E16398" 29900 c" D190" new-employee insert-ordered > > drop constant test-data > > > It was too difficult to extract the data from a string or a file. I have READ-SEQ that reads a text file into a list, and I have SPLIT that splits a comma-delimited string into a list, so no it was not too difficult for me --- it would have been trivial (all of this stuff was trivial). I think that the purpose of RosettaCode is not for me to demonstrate as many features of the novice package as I can in each program --- the purpose is for me to demonstrate how I would solve the given problem (the TOP function). I expect my code to be compared to the C code, so my code does the same thing as the C code. If WJ knew anything about ANS-Forth, he would point out that having more than one C" string in use at one time, is non-standard. SwiftForth only allows one C" at a time, whereas I have three in use at one time (the employee name, employee id, and employee department) on each line. The reason why ANS-Forth programmers are limited to one C" in use at a time, is because Elizabeth Rather can't imagine a program that works with more than one string, because none of the "Starting Forth" programs had more than one string in use at a time. This is also why PAD is in the ANS-Forth standard --- because Elizabeth Rather assumes that only one one string will be in use at any given time --- this is pretty pathetic, but this is what happens when a person's knowledge of a programming language is derived entirely from a comic book. WJ is so ignorant of ANS-Forth, that he fails to criticize the obvious failings of ANS-Forth (only one C" string at a time) --- instead he claims that I don't have a SPLIT function in my novice package, which I do have --- he is not even good at being a troll, which is generally considered to be an easier job than being a programmer. > > cr .( N = 0 ) > > 0 test-data top dup show-employee kill-employee > > > > cr .( N = 1 ) > > 1 test-data top dup show-employee kill-employee > > > > cr .( N = 2 ) > > 2 test-data top dup show-employee kill-employee > > > > cr .( N = 3 ) > > 3 test-data top dup show-employee kill-employee > > > > test-data kill-employee > > No output? No testing?
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2014-12-21 17:14 -0800 |
| Message-ID | <86855e78-9964-47b4-9432-651249b8ecc5@googlegroups.com> |
| In reply to | #35019 |
This code is an example of using a lambda function (the <TOP> function) that accesses the data on the parameter stack of the higher-order function --- it is not anonymous like a quotation, but it supports the basic idea. I would be interested in seeing Gerry Jackson write a version of this program using his LAMBDA.FTH code. I don't think it can go into RosettaCode because LAMBDA.FTH isn't publicly available anywhere (at least not until my next novice-package release when it will be included). Still though, it would be interesting to see --- I would include it along with LAMBDA.FTH etc. as an example program --- readers could compare it to my TopRank.4th program.
[toc] | [prev] | [next] | [standalone]
| From | Gerry Jackson <gerry@jackson9000.fsnet.co.uk> |
|---|---|
| Date | 2014-12-22 10:27 +0000 |
| Message-ID | <m78rko$ito$1@dont-email.me> |
| In reply to | #35089 |
On 22/12/2014 01:14, hughaguilar96@gmail.com wrote: > This code is an example of using a lambda function (the <TOP> function) that accesses the data on the parameter stack of the higher-order function --- it is not anonymous like a quotation, but it supports the basic idea. > > I would be interested in seeing Gerry Jackson write a version of this program Which program? The one you've posted on Rosetta code http://rosettacode.org/wiki/Top_rank_per_group#Forth Don't hold your breath, I haven't time to even think about it before Christmas and probably the New Year. using his LAMBDA.FTH code. I don't think it can go into RosettaCode because LAMBDA.FTH isn't publicly available anywhere (at least not until my next novice-package release when it will be included). Still though, it would be interesting to see --- I would include it along with LAMBDA.FTH etc. as an example program --- readers could compare it to my TopRank.4th program. > -- Gerry
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2014-12-22 22:03 -0800 |
| Message-ID | <cad28ee9-a80b-4b60-9012-75bb227bee4d@googlegroups.com> |
| In reply to | #35098 |
On Monday, December 22, 2014 3:26:55 AM UTC-7, Gerry wrote: > On 22/12/2014 01:14, hughaguilar96@gmail.com wrote: > > This code is an example of using a lambda function (the <TOP> function) that accesses the data on the parameter stack of the higher-order function --- it is not anonymous like a quotation, but it supports the basic idea. > > > > I would be interested in seeing Gerry Jackson write a version of this program > > Which program? The one you've posted on Rosetta code > http://rosettacode.org/wiki/Top_rank_per_group#Forth Yes --- that one is pretty simple (a linked-list or an array), somewhat real-worldish (more so than most of the RosettaCode problems), and it involves a lambda function that needs access to the upstream data (what your LAMBDA.FTH does). > Don't hold your breath, I haven't time to even think about it before > Christmas and probably the New Year. I didn't expect you to skip Christmas dinner to do it --- none of this stuff is very important --- do it whenever you get around to it, and I will include it in the next novice-package release (last one was 4 years ago).
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2015-01-08 17:17 -0800 |
| Message-ID | <076cb2b8-d0bd-4260-a068-6e01c9501d77@googlegroups.com> |
| In reply to | #35098 |
On Monday, December 22, 2014 at 3:26:55 AM UTC-7, Gerry wrote: > On 22/12/2014 01:14, hughaguilar96@gmail.com wrote: > > I would be interested in seeing Gerry Jackson write a version of this program > > Which program? The one you've posted on Rosetta code > http://rosettacode.org/wiki/Top_rank_per_group#Forth > > Don't hold your breath, I haven't time to even think about it before > Christmas and probably the New Year. > > using his LAMBDA.FTH code. I don't think it can go into RosettaCode > because LAMBDA.FTH isn't publicly available anywhere (at least not until > my next novice-package release when it will be included). Still though, > it would be interesting to see --- I would include it along with > LAMBDA.FTH etc. as an example program --- readers could compare it to my > TopRank.4th program. Any progress? BTW: I will include an upgraded version of this program that supports inheritance --- what I have now doesn't, because <TOP> only works on EMPLOYEE structs, but not on any struct derived from EMPLOYEE.
[toc] | [prev] | [next] | [standalone]
| From | Gerry Jackson <gerry@jackson9000.fsnet.co.uk> |
|---|---|
| Date | 2015-01-09 09:56 +0000 |
| Message-ID | <m8o8j4$f1r$1@dont-email.me> |
| In reply to | #35618 |
On 09/01/2015 01:17, hughaguilar96@gmail.com wrote: > On Monday, December 22, 2014 at 3:26:55 AM UTC-7, Gerry wrote: >> On 22/12/2014 01:14, hughaguilar96@gmail.com wrote: >>> I would be interested in seeing Gerry Jackson write a version of this program >> >> Which program? The one you've posted on Rosetta code >> http://rosettacode.org/wiki/Top_rank_per_group#Forth >> >> Don't hold your breath, I haven't time to even think about it before >> Christmas and probably the New Year. >> >> using his LAMBDA.FTH code. I don't think it can go into RosettaCode >> because LAMBDA.FTH isn't publicly available anywhere (at least not until >> my next novice-package release when it will be included). Still though, >> it would be interesting to see --- I would include it along with >> LAMBDA.FTH etc. as an example program --- readers could compare it to my >> TopRank.4th program. > > Any progress? I'm procrastinating -- Gerry
[toc] | [prev] | [next] | [standalone]
| From | hughaguilar96@gmail.com |
|---|---|
| Date | 2015-03-16 19:11 -0700 |
| Message-ID | <4f99594b-b755-4111-b046-d9e099160805@googlegroups.com> |
| In reply to | #35089 |
On Sunday, December 21, 2014 at 6:14:45 PM UTC-7, hughag...@gmail.com wrote: > This code is an example of using a lambda function (the <TOP> function) that accesses the data on the parameter stack of the higher-order function --- it is not anonymous like a quotation, but it supports the basic idea. This was a typo --- I meant: "accesses the data on the parameter stack of the calling function" --- the higher-order function EACH hides it's data on the return-stack so that it is NOT on the parameter-stack. I noticed that Franck has posted a version using his Oforth language: http://rosettacode.org/wiki/Top_Rank_Per_Group#Oforth I'm still trying to figure out how it works --- apparently #[ ... ] is a quotation --- I suppose I should read the manual... Franck's code was certainly more concise than my code --- that is true of pretty much every language that was used for this example problem, though.
[toc] | [prev] | [next] | [standalone]
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Date | 2014-12-24 05:25 +0000 |
| Message-ID | <m7dip0$oj9$1@dont-email.me> |
| In reply to | #34971 |
WJ wrote: > > > Gauche Scheme: > (use gauche.record) > ;; This will let us treat a list as though it is a structure (record). > (define-record-type (employee (pseudo-rtd <list>)) #t #t > name id salary dept)
[toc] | [prev] | [next] | [standalone]
| From | mix <mix@test.net> |
|---|---|
| Date | 2014-12-14 11:55 +0000 |
| Message-ID | <m6jtqt$faq$1@news.mixmin.net> |
| In reply to | #34847 |
<hughaguilar96@gmail.com> wrote: > On Wednesday, February 12, 2014 6:37:15 PM UTC-7, Julian Fondren wrote: >> 1. brute-force the answer with high-level data structures: walk the table, >> adding departments as keys to a hash table, the values of which are lists of >> salaries. Once you've exhausted the table, sort the keys of the hash (for nice >> output) and then print them and their top N of the sorted lists of salaries. >> >> (If you've never used a Perl oneliner that you wrote in a minute to chew on >> multiple repquota streams and spit out the top diskspace-using users, then that >> I call this a 'good solution' may puzzle you.) > > A program like this is trivial using my ASSOCIATION.4TH in the novice package: > http://www.forth.org/novice.html > It uses an LLRB tree internally, and it allows associations (or subsets > of associations) to be traversed in order. I would just build one > association sorted by department, then for each department I would build > an association sorted by salary, then just dump out the leftmost N nodes. > For a real program that has a lot of data and has to be robust, it would > make more sense to build an association sorted by name, and then build > associations sorted by department, salary, etc. in which the record is a > pointer to a node in the main association --- like a relational-database, > this saves memory by not replicating data. > > Note that I have SPLIT that converts comma-deliminated strings into lists > of fields. This works for any deliminator, so it can first break up the > comma-deliminated strings into a list, and then break up the name field > into a sublist of first-name and last-name. Also, it allows text to be in > quotes, so everything in the quotes will be considered a field even if it > contains the delimiter. > > I don't think Rosetta Stone allows the use of libraries however --- so it > favors languages such as Perl that have data-structures built in --- if > the programmer has to implement all of these data-structures from scratch > as Elizabeth Rather demands her cult members to do, the program is going > to be huge and complicated. It will also likely use an inefficient > data-structure such as a basic binary-tree rather than an LLRB tree, > because implementing an LLRB tree for a single program is way too much work. Who is Elizabeth Rather? > Actually, implementing a basic binary tree with insert and delete is a > lot of work too, if it has to be done from scratch for every program. > This is why ANS-Forth is never used in commercial programming --- > implementing everything from scratch is just too much work --- the boss > expects a trivial program like this to be done in an hour or two, and he > will fire a programmer who spends a week on it and then says: "Well, um, > I'm bogged down in figuring out how LLRB trees work, as they are quite complicated!" -- mix x
[toc] | [prev] | [next] | [standalone]
| From | empty-buffers <empty.buffersYOU-DONT-WANT-THIS@gmail.NEITHER-THIS.com> |
|---|---|
| Date | 2014-12-14 13:52 +0000 |
| Message-ID | <slrnm8r5he.7ce.empty.buffersYOU-DONT-WANT-THIS@Ferrycy.localdomain> |
| In reply to | #34917 |
In comp.lang.forth, mix wrote: >> as Elizabeth Rather demands her cult members to do > Who is Elizabeth Rather? According to Hugh - she's a goddess. :) -- May the Forth be with you!
[toc] | [prev] | [next] | [standalone]
| From | mix <mix@test.net> |
|---|---|
| Date | 2014-12-14 11:58 +0000 |
| Message-ID | <m6ju0j$fgs$1@news.mixmin.net> |
| In reply to | #34847 |
<hughaguilar96@gmail.com> wrote: > On Wednesday, February 12, 2014 6:37:15 PM UTC-7, Julian Fondren wrote: >> 1. brute-force the answer with high-level data structures: walk the table, >> adding departments as keys to a hash table, the values of which are lists of >> salaries. Once you've exhausted the table, sort the keys of the hash (for nice >> output) and then print them and their top N of the sorted lists of salaries. >> >> (If you've never used a Perl oneliner that you wrote in a minute to chew on >> multiple repquota streams and spit out the top diskspace-using users, then that >> I call this a 'good solution' may puzzle you.) > > A program like this is trivial using my ASSOCIATION.4TH in the novice package: > http://www.forth.org/novice.html > It uses an LLRB tree internally, and it allows associations (or subsets > of associations) to be traversed in order. I would just build one > association sorted by department, then for each department I would build > an association sorted by salary, then just dump out the leftmost N nodes. > For a real program that has a lot of data and has to be robust, it would > make more sense to build an association sorted by name, and then build > associations sorted by department, salary, etc. in which the record is a > pointer to a node in the main association --- like a relational-database, > this saves memory by not replicating data. > > Note that I have SPLIT that converts comma-deliminated strings into lists > of fields. This works for any deliminator, so it can first break up the > comma-deliminated strings into a list, and then break up the name field > into a sublist of first-name and last-name. Also, it allows text to be in > quotes, so everything in the quotes will be considered a field even if it > contains the delimiter. > > I don't think Rosetta Stone allows the use of libraries however --- so it > favors languages such as Perl that have data-structures built in --- if > the programmer has to implement all of these data-structures from scratch > as Elizabeth Rather demands her cult members to do, the program is going > to be huge and complicated. It will also likely use an inefficient > data-structure such as a basic binary-tree rather than an LLRB tree, > because implementing an LLRB tree for a single program is way too much work. Who is Elizabeth Rather? > Actually, implementing a basic binary tree with insert and delete is a > lot of work too, if it has to be done from scratch for every program. > This is why ANS-Forth is never used in commercial programming --- > implementing everything from scratch is just too much work --- the boss > expects a trivial program like this to be done in an hour or two, and he > will fire a programmer who spends a week on it and then says: "Well, um, > I'm bogged down in figuring out how LLRB trees work, as they are quite complicated!" -- mix x
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.forth
csiph-web