Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Paul Rubin Newsgroups: comp.lang.forth Subject: Re: How could have I factored this better? Date: Fri, 20 May 2016 15:44:20 -0700 Organization: A noiseless patient Spider Lines: 29 Message-ID: <871t4we4e3.fsf@jester.gateway.pace.com> References: <5e60dbda-f34b-4be0-bfac-d32ddc67062f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="52eac348160ce62b68e4635eee7cc854"; logging-data="25563"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0Wc17KN3baYSTJtXd2LVk" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Cancel-Lock: sha1:LV9Y8xwNgBXtFYzWFqjsCQrS9mw= sha1:Vr6lTb9KYtJMAw3+6uQdstKyY3Q= Xref: csiph.com comp.lang.forth:47370 Darin Murphy writes: > Hi everybody. I created the following program and would like to know > how I could have factored and documented it better. Here's my version, which possibly uses some gforth words: : checkneg ( n -- ) 0 < abort" need a non-negative integer" ; : star ( -- ) '*' emit ; : stars ( n -- ) dup checkneg 0 ?do star loop ; : solid-box-stars { cols rows } cr cols checkneg rows checkneg rows 0 ?do cols stars cr loop ; Differences from yours: 1. The stack comment in your STAR word is ( n -- ) but should be ( -- ) 2. I used ABORT to break out of the error check since the number is invalid and there's no way to continue. In a bigger program you might want to catch the error with CATCH. 3. I checked for < 0 instead of < 1, since it seems perfectly ok to me to print zero stars without it being an error. That meant changing the loops to ?DO. The repeat/while stuff was clumsy. 4. I used gforth locals for the args to solid-box-stars. Maybe that's controversial but it avoids some stack juggling, assuming you want the interface to stay what you wrote.