Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #47277

Re: fizzbuzz problem in forth

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: fizzbuzz problem in forth
Date 2016-05-18 12:36 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2016May18.143614@mips.complang.tuwien.ac.at> (permalink)
References (2 earlier) <871t566dwf.fsf@jester.gateway.pace.com> <2016May13.145248@mips.complang.tuwien.ac.at> <8760udodtl.fsf@jester.gateway.pace.com> <2016May17.154948@mips.complang.tuwien.ac.at> <87k2ismu0z.fsf@jester.gateway.pace.com>

Show all headers | View raw


Paul Rubin <no.email@nospam.invalid> writes:
>anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
>>  In some cases, print something else; if you don't print something
>>  else, print the number.
>> Not sure why you smell something here.
>
>Because that description is wrong.  It's more like "in some cases, print
>some specified string.  Otherwise, print the number".  The trouble is
>that the specified string might be the empty string.

Not in the original task and not in the extended versions suggested by
<http://dave.fayr.am/posts/2012-10-4-finding-fizzbuzz.html> nor those
discussed in other postings I found, either.

So, ok, your solution, my "more Forth-like solution" and the Haskell
Maybe variant are prepared for the case when the extended task says:
"In some cases, print nothing."  But is it worth complicating the
solution with Maybe and (up-to-now) redundant flags in order to be
prepared for this?  What about other potential changes?  Say, skipping
the next number on some condition; or changing the condition for
printing "buzz" when some condition holds.

Anyway, one of Chuck Moore's principles is "Do what is needed, don't
generalize", and this discussion is a good example for that.  Whatever
solution you write, the interviewer (or, in real life, the customer)
can give you a change request for which it does not work well.  And
you can "smell" that, so every solution will have a smell if you are
thinking in terms of what-ifs.

Back to the >STRING-EXECUTE implementation.  I thought about a simpler
way (as far as implementation is concerned).  Fig-Forth has a user
variable OUT that is incremented whenever EMIT is called (or, by
extension, TYPE).  If a Forth system has OUT (and it works that way),
there is no need to collect the output in a string to determine it's
length, you can use OUT for it.  The solution would look as follows:

: fizzbuzz1 ( n -- )
    dup 3 mod 0= if ." fizz" then
    dup 5 mod 0= if ." buzz" then
    drop ;

: fizzbuzz ( n -- )
    cr 0 out ! dup fizzbuzz1 out @ if drop else . then ;

: main ( -- )
    101 1 ?do i fizzbuzz loop ;

Works nicely on VFXlin.  However, Gforth and SwiftForth don't have
OUT.

- 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


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