Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Let's play golf

126 views
Skip to first unread message

WJ

unread,
Jul 19, 2016, 7:29:07 AM7/19/16
to
Task:

Convert "Cat ate mouse." to "Mouse ate cat.", using
chop, split, reverse, join, capitalize, and
string-concatenation.

"Cat ate mouse.".chop
==>"Cat ate mouse"
"Cat ate mouse.".chop.split
==>["Cat", "ate", "mouse"]
"Cat ate mouse.".chop.split.reverse
==>["mouse", "ate", "Cat"]
"Cat ate mouse.".chop.split.reverse.join(" ")
==>"mouse ate Cat"
"Cat ate mouse.".chop.split.reverse.join(" ").capitalize
==>"Mouse ate cat"
"Cat ate mouse.".chop.split.reverse.join(" ").capitalize + "."
==>"Mouse ate cat."

Can it be shorter in ANS Forth?

--
If confirmed, Garland would be the fourth Jewish justice on the nation's
highest court, which is comprised entirely of Jews and Catholics. The three
current Jewish members of the Supreme Court are Ruth Bader Ginsburg, Elana
Kagan, and Stephen Breyer.
www.jta.org/2016/03/16/news-opinion/united-states/obama-to-name-jewish-federal-judge-to-supreme-court

hughag...@gmail.com

unread,
Jul 19, 2016, 5:07:25 PM7/19/16
to
On Tuesday, July 19, 2016 at 4:29:07 AM UTC-7, WJ wrote:
> Task:
>
> Convert "Cat ate mouse." to "Mouse ate cat.", using
> chop, split, reverse, join, capitalize, and
> string-concatenation.
>
> "Cat ate mouse.".chop
> ==>"Cat ate mouse"
> "Cat ate mouse.".chop.split
> ==>["Cat", "ate", "mouse"]
> "Cat ate mouse.".chop.split.reverse
> ==>["mouse", "ate", "Cat"]
> "Cat ate mouse.".chop.split.reverse.join(" ")
> ==>"mouse ate Cat"
> "Cat ate mouse.".chop.split.reverse.join(" ").capitalize
> ==>"Mouse ate cat"
> "Cat ate mouse.".chop.split.reverse.join(" ").capitalize + "."
> ==>"Mouse ate cat."
>
> Can it be shorter in ANS Forth?

Here it is using my LIST.4TH and STRING-STACK.4TH packages:

s" Cat ate mouse" ok-2
2dup uncapitalize ok-2
bl char " char " <split> ok-1
reverse ok-1
bl combine ok-1
count ok-2
2dup capitalize ok-2
2dup type Mouse ate cat ok-2

Note that I had to write CAPITALIZE because I didn't have that one, but it was trivial. It is in STRING-STACK.4TH now.

Note also that my SPLIT is much more sophisticated that the split function that WJ is using. I can do things like this:

s" <Aguilar, Hugh>, Pelc, " char , char < char > <split> ok-1
show-seq
Aguilar, Hugh
Pelc

ok

Note also that WJ is not a programmer. He is just a script-kiddie. He finds code-libraries and he learns how to use them, but he is not capable of writing code-libraries himself. By comparison, I wrote these code-libraries myself that I am using.

> If confirmed, Garland would be the fourth Jewish justice on the nation's
> highest court, which is comprised entirely of Jews and Catholics. The three
> current Jewish members of the Supreme Court are Ruth Bader Ginsburg, Elana
> Kagan, and Stephen Breyer.
> www.jta.org/2016/03/16/news-opinion/united-states/obama-to-name-jewish-federal-judge-to-supreme-court

WJ is a jackass --- I recommend that nobody ever respond to any of his posts --- I only responded to this one because it was trivial, but as a matter of principle I generally ignore him.

HAA

unread,
Jul 21, 2016, 4:29:10 AM7/21/16
to
hughag...@gmail.com wrote:
> ...
> Note also that WJ is not a programmer. He is just a script-kiddie. He finds
> code-libraries and he learns how to use them, but he is not capable of writing
> code-libraries himself.

He could just as easily argue portable code libraries was the reason behind
ANS-Forth. If Forthers haven't written the libraries he uses - preferring instead
to endlessly chatter and fight over what should be in the *next* standard - it is
a problem he doesn't have.



Doug Hoffman

unread,
Jul 22, 2016, 9:00:06 AM7/22/16
to
On 7/19/16 5:07 PM, hughag...@gmail.com wrote:

> Here it is using my LIST.4TH and STRING-STACK.4TH packages:
>
> s" Cat ate mouse" ok-2
> 2dup uncapitalize ok-2
> bl char " char " <split> ok-1
> reverse ok-1
> bl combine ok-1
> count ok-2
> 2dup capitalize ok-2
> 2dup type Mouse ate cat ok-2

With an objects extension and library:

${ Cat ate mouse } ok \ 1
dup reverse: ok \ 1
dup last: lower: ok \ 1
dup first: capitalize: ok \ 1
dup p: ${ Mouse ate cat } ok \ 1


But there was a period to deal with:

${ Cat ate mouse. } ok \ 1
dup reverse: ok \ 1
dup last: dup lower: '.' swap +: ok \ 1
dup first: dup trim: capitalize: ok \ 1
dup p: ${ Mouse ate cat. } ok \ 1


The nice thing about using objects here (for handling a list of strings)
is the code was easily derived by inheriting from existing
dynamically sizeable list and string classes. In a similar manner it was
easy to have lists of integers, floats, and any objects. All derived
(inherited) from the same base class. Easy re-use of existing code.

The other nice thing about objects is the ability to re-use words
(messages) for different data types. reverse: last: +: first: and p: are
all messages common to many different object types and do what one would
expect for each.

Naturally there are lots of different ways to approach this kind of
problem. I had already written the library code quite awhile ago so I
used that.

-Doug

hughag...@gmail.com

unread,
Jul 22, 2016, 5:04:35 PM7/22/16
to
I agree that inheritance is fundamental to any general-purpose data-structure.

> The other nice thing about objects is the ability to re-use words
> (messages) for different data types. reverse: last: +: first: and p: are
> all messages common to many different object types and do what one would
> expect for each.

I disagree that polymorphism is necessary --- this kills the speed --- it is just a convenience, so I don't think it is worth the cost.

There are sometimes name clashes though. I noticed when writing this that I have REVERSE in string-stack.4th (reverses the chars in a string) and also have REVERSE in list.4th (reverses the elements in a list), so I had to change the string-stack.4th one to REVERSE-STR instead --- I had never used the two packages together before and hadn't noticed the clash.

> Naturally there are lots of different ways to approach this kind of
> problem. I had already written the library code quite awhile ago so I
> used that.

What library is this?

My method certainly gets the prize for being the simplest Forth OOP package --- it is not really a "package" but rather it is just a style of programming --- other than FIELD and ALLOCATION (used for cloning data-structures) there isn't anything to it.

Doug Hoffman

unread,
Jul 23, 2016, 3:20:03 AM7/23/16
to
On 7/22/16 5:04 PM, hughag...@gmail.com wrote:

> What library is this?

My personal library.

> My method certainly gets the prize for being the simplest Forth OOP
> package --- it is not really a "package" but rather it is just a style
> of programming --- other than FIELD and ALLOCATION (used for cloning
> data-structures) there isn't anything to it.

Sounds good. You should stick with that.

-Doug
0 new messages