Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #47085
| From | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: fizzbuzz problem in forth |
| Date | 2016-05-13 12:52 +0000 |
| Organization | Institut fuer Computersprachen, Technische Universitaet Wien |
| Message-ID | <2016May13.145248@mips.complang.tuwien.ac.at> (permalink) |
| References | <2210aa04-1df4-4328-bf8c-66fbf845609c@googlegroups.com> <f2c2561c-5208-452d-97b7-3aff17789c02@googlegroups.com> <871t566dwf.fsf@jester.gateway.pace.com> |
Paul Rubin <no.email@nospam.invalid> writes:
>That fizzbuzz problem attracts so many solutions not
>because it's difficult, but because it has annoying edges that draw
>people irresistably to look for ways to smooth them down.
>
>http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html
>(not Forth related) expands on that thought.
I remember that someone presented a nice, concise solution one of the
last times this came up. Anyway, the article you linked to inspired
the following solution:
: fizzbuzz1 ( n -- )
dup 3 mod 0= if ." fizz" then
dup 5 mod 0= if ." buzz" then
drop ;
: fizzbuzz ( n -- )
cr dup ['] fizzbuzz1 >string-execute dup if
2dup type drop free throw drop
else
drop free throw . then ;
: main ( -- )
101 1 ?do i fizzbuzz loop ;
(unveiled a bug in RESIZE, and I cannot commit the fix at the moment).
Of course, using >STRING-EXECUTE to find out if the output of
FIZZBUZZ1 is empty is very heavyweight. A more Forth-like solution
is:
: fizzbuzz1 ( n -- f )
true
over 3 mod 0= if ." fizz" drop false then
over 5 mod 0= if ." buzz" drop false then
nip ;
: fizzbuzz ( n -- )
cr dup fizzbuzz1 if . else drop then ;
: main ( -- )
101 1 ?do i fizzbuzz loop ;
Simpler, faster, takes a little more effort than the earlier variant
to add more cases, but that's in the Forth spirit of not
overgeneralizing.
- 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 2016: http://www.euroforth.org/ef16/
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
fizzbuzz problem in forth murphy.darin@gmail.com - 2016-05-12 16:17 -0700
Re: fizzbuzz problem in forth Paul Rubin <no.email@nospam.invalid> - 2016-05-12 16:43 -0700
Re: fizzbuzz problem in forth murphy.darin@gmail.com - 2016-05-12 17:22 -0700
Re: fizzbuzz problem in forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-13 12:52 +0000
Re: fizzbuzz problem in forth Paul Rubin <no.email@nospam.invalid> - 2016-05-16 15:09 -0700
Re: fizzbuzz problem in forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-17 13:49 +0000
Re: fizzbuzz problem in forth Paul Rubin <no.email@nospam.invalid> - 2016-05-17 11:14 -0700
Re: fizzbuzz problem in forth anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-05-18 12:36 +0000
Re: fizzbuzz problem in forth Paul Rubin <no.email@nospam.invalid> - 2016-05-20 21:55 -0700
Re: fizzbuzz problem in forth julian.fondren@endurance.com - 2016-06-20 20:20 -0700
Re: fizzbuzz problem in forth hughaguilar96@gmail.com - 2016-05-17 12:26 -0700
Re: fizzbuzz problem in forth Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2016-05-15 21:42 +0100
Re: fizzbuzz problem in forth hughaguilar96@gmail.com - 2016-05-15 14:41 -0700
Re: fizzbuzz problem in forth hughaguilar96@gmail.com - 2016-05-15 15:15 -0700
Re: fizzbuzz problem in forth hughaguilar96@gmail.com - 2016-05-15 17:38 -0700
Re: fizzbuzz problem in forth murphy.darin@gmail.com - 2016-05-15 19:01 -0700
Re: fizzbuzz problem in forth hughaguilar96@gmail.com - 2016-05-15 19:51 -0700
Re: fizzbuzz problem in forth murphy.darin@gmail.com - 2016-05-15 20:48 -0700
Re: fizzbuzz problem in forth hughaguilar96@gmail.com - 2016-05-16 07:51 -0700
Re: fizzbuzz problem in forth "WJ" <w_a_x_man@yahoo.com> - 2016-05-25 08:31 +0000
Re: fizzbuzz problem in forth "WJ" <w_a_x_man@yahoo.com> - 2016-05-27 19:06 +0000
Re: fizzbuzz problem in forth humptydumpty <ouatubi@gmail.com> - 2016-05-18 13:19 -0700
Re: fizzbuzz problem in forth hughaguilar96@gmail.com - 2016-05-18 17:49 -0700
Re: fizzbuzz problem in forth humptydumpty <ouatubi@gmail.com> - 2016-05-18 21:17 -0700
Re: fizzbuzz problem in forth hughaguilar96@gmail.com - 2016-05-19 07:38 -0700
Re: fizzbuzz problem in forth "Kerr Mudd-John" <admin@127.0.0.1> - 2016-06-20 19:24 -0100
Re: fizzbuzz problem in forth endlessboomcapitalism@gmail.com - 2016-06-20 15:03 -0700
Re: fizzbuzz problem in forth Albert van der Horst <albert@cherry.spenarnc.xs4all.nl> - 2016-05-19 13:07 +0000
Re: fizzbuzz problem in forth Paul Rubin <no.email@nospam.invalid> - 2016-05-19 12:22 -0700
Re: fizzbuzz problem in forth Albert van der Horst <albert@cherry.spenarnc.xs4all.nl> - 2016-05-20 23:51 +0000
csiph-web