Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.forth Subject: Re: fizzbuzz problem in forth Date: Mon, 16 May 2016 15:09:10 -0700 Organization: A noiseless patient Spider Lines: 26 Message-ID: <8760udodtl.fsf@jester.gateway.pace.com> References: <2210aa04-1df4-4328-bf8c-66fbf845609c@googlegroups.com> <871t566dwf.fsf@jester.gateway.pace.com> <2016May13.145248@mips.complang.tuwien.ac.at> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="589dd00be5ee573d7046ed589be3c50c"; logging-data="20302"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18/1jpNoMpIOTNWSAti51Qu" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:6wbyNZsVNiOayFGtUvzAa8rD3Nc= sha1:3AROlrQJiXSNFaC7OI1vpqrIBRM= Xref: csiph.com comp.lang.forth:47221 anton@mips.complang.tuwien.ac.at (Anton Ertl) writes: > Of course, using >STRING-EXECUTE to find out if the output of > FIZZBUZZ1 is empty is very heavyweight. Yes, and also mathematically unsatisfying imho. The pattern in generalized fizzbuzz is "IF this-divisor THEN that-string" and making the empty string special is a code smell. I've seen a number of e.g. Python solutions like that, which don't seem great for the same reason. > 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 ; ... Here's my solution from some time back: : fout ( addr u n d -- flag ) mod 0= if type 0 else 2drop 1 then ; : fb.1 ( n -- flag ) >r s" Fizz" r@ 3 fout s" Buzz" r> 5 fout and ; : fb ( n -- ) dup fb.1 if . else drop then cr ; : run ( -- ) 20 1 do i fb loop ;