Message from discussion
Recursion
Received: by 10.68.125.233 with SMTP id mt9mr6746819pbb.5.1333724117136;
Fri, 06 Apr 2012 07:55:17 -0700 (PDT)
MIME-Version: 1.0
Path: r9ni25538pbh.0!nntp.google.com!news2.google.com!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail
From: an...@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.lang.forth
Subject: Re: Recursion
Date: Fri, 06 Apr 2012 14:47:32 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
Lines: 28
Message-ID: <2012Apr6.164732@mips.complang.tuwien.ac.at>
References: <0411f347-b5ae-46ab-8b63-574b2c032308@f5g2000vbt.googlegroups.com> <7xsjghgxzu.fsf@ruckus.brouhaha.com> <HZidnfEaqJERTePSnZ2dnUVZ_vqdnZ2d@supernews.com>
Injection-Info: mx04.eternal-september.org; posting-host="42AvmJhM5u/xQ8v/vurAFA";
logging-data="1048"; mail-complaints-to="ab...@eternal-september.org"; posting-account="U2FsdGVkX18chezP0hD9NbWYZ+xus78i"
X-newsreader: xrn 10.00-beta-3
Cancel-Lock: sha1:X5V/MPd/kBij7EsqUVA/pLEcnhE=
Andrew Haley <andre...@littlepinkcloud.invalid> writes:
>Paul Rubin <no.em...@nospam.invalid> wrote:
>> This relates to Euler problem 303. Enumerate in increasing order, the
>> numbers that can be written in base 10 using just the digits 0, 1, and 2.
>>
>> : r ( n -- n ) 3 /mod dup if recurse 10 * + else drop then ;
>> : test ( -- ) 50 0 do i r . loop ;
>
>Surely for a recursion exaple to be meaningful it has to be something
>that actually requires backtracking, such as a tree walk. This
>example is better done with a loop:
>
>: test 3 base ! 50 0 do i . loop ;
If you want just the printout, that's a solution, but in Euler303 you
want to do something else with the numbers; note the "in base 10" in
the partial problem statement above.
I used BASE, <# #s #> and >number to do this part, but my code is not
smaller (and probably slower), so one can argue that recursion is used
productively here.
- 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 2011: http://www.euroforth.org/ef11/