Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #45581
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: Rosetta code reverse a string |
| Date | 2016-02-16 16:22 +0000 |
| Organization | Institut fuer Computersprachen, Technische Universitaet Wien |
| Message-ID | <2016Feb16.172247@mips.complang.tuwien.ac.at> (permalink) |
| References | <e69b019d-9102-4232-b3e0-c6b524056bfd@googlegroups.com> <87d1rx8t35.fsf@jester.gateway.pace.com> <2016Feb16.144414@mips.complang.tuwien.ac.at> <56c335f7$0$23792$e4fe514c@news.xs4all.nl> |
Albert van der Horst <albert@spenarnc.xs4all.nl> writes:
>anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>There is an improvement possible using the technique described by John
>>Hayes in the SigForth Newsletter 4(2), Fall '92, for implementing {
>>for locals (and improved in the Forth-2012 reference implementation):
>
>>: revline ( "line" -- )
>> parse-name dup if
>> recurse type space exit then
>> 2drop ;
>
>>: >> ( "line" -- )
>> revline cr ;
>
>>Here REVLINE just reverses the rest of the line before printing the
>>token it has parsed. Note that PARSE-NAME cannot be replaced with BL
>>WORD COUNT here, because several invocations of WORD may use (and
>>typically do use) the same buffer.
>
>>This example is also nice for demonstrating that we can use the
>>implicit input through the input stream to make the stack handling
>>simpler, and can then turn that into an on-stack input with
>>EXECUTE-PARSING.
>
>>: .reverse ( c-addr u -- )
>> ['] revline execute-parsing ;
>
>>Likewise, we can turn the implicit output to the user terminal into an
>>explicit output on the stack with >STRING-EXECUTE (Gforth development
>>version):
>
>>: reverse-string ( c-addr1 u1 -- c-addr2 u2 )
>> ['] .reverse >string-execute ;
>
>>Compare this to what you would have to do to program this without
>>going through the input stream and without using the implicit-output
>>words TYPE and SPACE.
>
>Let's compare it with a Forth that has lifted strings to a
>very low, but practical abstraction level.
>The ministring package ($@ $! $C+ $/ $^ ) saves the day (again).
>
>Note that the first reverse is the original solution that uses
>the implicit-output words.
>
>-------------------------------
>( ciforth) WANT RDROP $@ $! $+! $C+ $/
>
>: TYPE PAD $+! ;
>: SPACE BL PAD $C+ ;
>
>\ Print SC in reverse order.
>: reverse DEPTH 2 - >R BEGIN BL $/ 2SWAP DUP 0= UNTIL
> BEGIN TYPE SPACE DEPTH R@ = UNTIL RDROP ;
>
>: reverse "" PAD $! reverse PAD $@ ;
>
>-------------------------------
So we have EXECUTE-PARSING and >STRING-EXECUTE vs. $@ $! $+! $C+ $/
(plus the use of PAD).
>Example of usage:
>
>"We gaan naar rome " reverse fd-output WRITE-FILE THROW
Of course, you can first put the result into a string and then output
it to a file, but with the approach I posted, you can also do it more
directly (with an additional word: OUTFILE-EXECUTE):
s" We gaan naar rome " ' .reverse stdout outfile-execute
- 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 2015: http://www.rigwit.co.uk/EuroForth2015/
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Rosetta code reverse a string foxaudioresearch@gmail.com - 2016-02-15 13:22 -0800
Re: Rosetta code reverse a string Paul Rubin <no.email@nospam.invalid> - 2016-02-15 15:14 -0800
Re: Rosetta code reverse a string foxaudioresearch@gmail.com - 2016-02-15 16:49 -0800
Re: Rosetta code reverse a string Paul Rubin <no.email@nospam.invalid> - 2016-02-15 20:27 -0800
Re: Rosetta code reverse a string anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-16 07:25 +0000
Re: Rosetta code reverse a string Doug Hoffman <glidedog@gmail.com> - 2016-02-16 05:30 -0500
Re: Rosetta code reverse a string Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-16 11:55 +0000
Re: Rosetta code reverse a string anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-16 13:44 +0000
Re: Rosetta code reverse a string Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-16 14:45 +0000
Re: Rosetta code reverse a string anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-16 16:22 +0000
Re: Rosetta code reverse a string Paul Rubin <no.email@nospam.invalid> - 2016-02-16 13:25 -0800
Re: Rosetta code reverse a string anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-02-17 07:07 +0000
Re: Rosetta code reverse a string Paul Rubin <no.email@nospam.invalid> - 2016-02-16 23:29 -0800
Re: Rosetta code reverse a string Albert van der Horst <albert@spenarnc.xs4all.nl> - 2016-02-16 11:46 +0000
Re: Rosetta code reverse a string "WJ" <w_a_x_man@yahoo.com> - 2016-02-16 18:25 +0000
Re: Rosetta code reverse a string humptydumpty <ouatubi@gmail.com> - 2016-02-16 13:40 -0800
Re: Rosetta code reverse a string "WJ" <w_a_x_man@yahoo.com> - 2016-06-28 01:55 +0000
Re: Rosetta code reverse a string "HAA" <someone@microsoft.com> - 2016-06-29 11:45 +1000
csiph-web