Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #23601
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: structure and interpretation of computer programs exercsie 1.3 in forth |
| Date | 2013-06-14 15:37 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <kpfdap$eqo$1@dont-email.me> (permalink) |
| References | (8 earlier) <e9ea8ae4-4b51-42cd-9a44-eca5ba9a8683@10g2000yqo.googlegroups.com> <ae2d5ea7-b58a-4a22-b929-f5a1331208f1@k6g2000yqf.googlegroups.com> <7xobgsp5u3.fsf@ruckus.brouhaha.com> <c330752a-7a1b-4e74-8f06-76b15379758a@x3g2000yqo.googlegroups.com> <2243495.gS2lSoF1Fd@sunwukong.fritz.box> |
Bernd Paysan wrote:
> Mark Wills wrote:
>
> > On Jan 14, 7:43 am, Paul Rubin <no.em...@nospam.invalid> wrote:
> >> Mark Wills <forthfr...@gmail.com> writes:
> >> > I also spent a while looking at doing it without conditionals, and
> >> > eventually concluded that it couldn't be done.
> > >
> >> : sq dup * ;
> >> : g { a b c -- n } a sq b sq + c sq + a b c min min sq - ;
> >
> > Well, okay, I stand corrected! Nice job.
>
> There are other ways:
>
> : sq ( n -- n² ) dup * ;
> : g ( a b c -- n ) 2dup max sq >r min max sq r> + ;
>
> > The fact that it needs locals, and computes all the squares is a bit
> > of an "ouch"!
>
> Yes. The solution without locals and without squaring all three numbers
> is shorter.
The solution without Forth and without using the return stack
is shorter, even when it is generalized to handle any number of
numbers.
Ruby:
g = proc{|x| a,b = x.sort.pop(2); a*a + b*b}
==>#<Proc:0x0297ea58@(irb):13>
g[ [5, 3, 33, 22, 333] ]
==>111978
Back to comp.lang.forth | Previous | Next | Find similar
Re: structure and interpretation of computer programs exercsie 1.3 in forth "WJ" <w_a_x_man@yahoo.com> - 2013-06-14 15:37 +0000
csiph-web