Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

System going down in 5 minutes.


devel / comp.lang.lisp / recursion is silly

SubjectAuthor
* recursion is sillynone
+* Re: recursion is sillyLIT
|+* Re: recursion is sillyJulieta Shem
||+* Re: recursion is sillyLIT
|||`- Re: recursion is sillyJulieta Shem
||+- Re: recursion is sillyLIT
||+* Re: recursion is sillyJoerg Mertens
|||`- Re: recursion is sillyJulieta Shem
||`- Re: recursion is sillynone
|`* Re: recursion is sillyGeorge Neuner
| +* Re: recursion is sillyKaz Kylheku
| |`* Re: recursion is sillyGeorge Neuner
| | `* Re: recursion is sillyKaz Kylheku
| |  +* Re: recursion is sillyPaul Rubin
| |  |+* Re: recursion is sillyJulieta Shem
| |  ||`* Re: recursion is sillyPaul Rubin
| |  || `* Re: recursion is sillyJulieta Shem
| |  ||  `* Re: recursion is sillyPaul Rubin
| |  ||   `- These jumps are enjoyable (was: recursion is silly)Axel Reichert
| |  |+* Re: recursion is sillyAnton Ertl
| |  ||`* Re: recursion is sillynone
| |  || `- Re: recursion is sillyAnton Ertl
| |  |`- Re: recursion is sillynone
| |  +- Re: recursion is sillyGeorge Neuner
| |  `* Re: recursion is sillynone
| |   `* Re: recursion is sillyPaul Rubin
| |    `* Re: recursion is sillynone
| |     +* Re: recursion is sillymhx
| |     |+* Re: recursion is sillyKaz Kylheku
| |     ||+* Re: recursion is sillyKaz Kylheku
| |     |||`- Re: recursion is sillynone
| |     ||`* Re: recursion is sillynone
| |     || `* Re: recursion is sillyKaz Kylheku
| |     ||  `* Re: recursion is sillymhx
| |     ||   `* Re: recursion is sillyKaz Kylheku
| |     ||    +* Re: recursion is sillyLawrence D'Oliveiro
| |     ||    |`* Re: recursion is sillyKaz Kylheku
| |     ||    | `- Re: recursion is sillyKaz Kylheku
| |     ||    `* Re: recursion is sillymhx
| |     ||     `- Re: recursion is sillyAnton Ertl
| |     |`- Re: recursion is sillyPaul Rubin
| |     `* Re: recursion is sillyPaul Rubin
| |      `- Re: recursion is sillynone
| +- Re: recursion is sillynone
| `* Re: recursion is sillydxf
|  `- Re: recursion is sillyGeorge Neuner
+- Re: recursion is sillyJulieta Shem
+* Re: recursion is sillyPaul Rubin
|`* Re: recursion is sillyPaul Rubin
| `* Re: recursion is sillyPaul Rubin
|  +* Re: recursion is sillymhx
|  |+- Re: recursion is sillyPaul Rubin
|  |+* Re: recursion is sillyPaul Rubin
|  ||`* Re: recursion is sillynone
|  || +* Re: recursion is sillyPaul Rubin
|  || |+- Re: recursion is sillynone
|  || |`- Re: recursion is sillynone
|  || `* Re: recursion is sillymhx
|  ||  +- Re: recursion is sillynone
|  ||  `* Re: recursion is sillynone
|  ||   `* Re: recursion is sillyPaul Rubin
|  ||    `* Re: recursion is sillynone
|  ||     `* Re: recursion is sillyPaul Rubin
|  ||      `- Re: recursion is sillyLawrence D'Oliveiro
|  |`- Re: recursion is sillynone
|  `- Re: recursion is sillynone
+- Re: recursion is sillyminforth
`* Re: recursion is sillyLawrence D'Oliveiro
 `* Re: recursion is sillyJulieta Shem
  +* Re: recursion is sillyLawrence D'Oliveiro
  |`* Re: recursion is sillyJulieta Shem
  | `* Re: recursion is sillyLawrence D'Oliveiro
  |  `* Re: recursion is sillyJulieta Shem
  |   `* Re: recursion is sillyLawrence D'Oliveiro
  |    `* Re: recursion is sillyJulieta Shem
  |     `* Re: goto is sillyLawrence D'Oliveiro
  |      `- Re: goto is sillyKaz Kylheku
  `- Re: recursion is sillyPaul Rubin

Pages:1234
recursion is silly

<nnd$6660ae2b$78615640@f2c7e8d01681877c>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17615&group=comp.lang.lisp#17615

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
Organization: KPN B.V.
Date: Wed, 27 Dec 2023 16:17:58 +0100
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder3.eternal-september.org!news.mixmin.net!feed.abavia.com!abe006.abavia.com!abp003.abavia.com!news.kpn.nl!not-for-mail
Lines: 83
Injection-Date: Wed, 27 Dec 2023 16:17:58 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
 by: none - Wed, 27 Dec 2023 15:17 UTC

Recursion is silly (if you can avoid it).

I recently draw attention to a toy lisp in Forth.
The example program was a scheme program that counts
how many changes in coins are possible for e.g. 100 dollarcent.
The lisp had to improved to be able to compile the program.
The improved version can hardly manage an input of 100 and takes
a noticable time, 200 and following fails.

A more sane program fills in a table where each entry is derived
from previous entries. A possibility to change for 100 cent is e.g.
adding 10 cent to the possibilities for 90 cent.
This is a kind of reversed dynamic programming.

In Forth it looks like.
\ --------------------- 8< ---------------------------
WANT VALUE
: _ 0 ; \ Don't care value
CREATE kind-of-coins 1 , 5 , 10 , 25 , 50 , 0 ,

_ VALUE limit
_ VALUE aap

\ One possibility to change 0 cent, others initially zero.
: init 1 , limit 0 DO 0 , LOOP ;

: aap[] CELLS aap + ;

\ Add possibilities for denomination to `aap
: add limit 1+ OVER DO I OVER - aap[] @ I aap[] +! LOOP DROP ;
: doit 1 ARG[] EVALUATE TO limit
HERE TO aap init
kind-of-coins BEGIN $@ DUP WHILE add REPEAT DROP
limit aap[] ? CR ;

\ --------------------- 8< ---------------------------
You may have not have to want VALUE.
$@ is another name for @+ / OUNT .
If you can't handle command line arguments, you can replace
`` 1 ARG[] EVALUATE '' with the limit you want.
Around 70 dollar you get overflow in 32 bit systems.
It is hard to measure speed in 32 bit systems,
suffice it to say that is certainly under 10 ms.

My forth (lina) is not the fastest, however

time coinsusa 1,000,000
666793341266850001
real 0m0.353s
user 0m0.281s
sys 0m0.029s

For reference this is the scheme program.
\ --------------------- 8< ---------------------------
(define (count-change amount)
(cc amount 5))
(define (cc amount kinds-of-coins)
(cond ((= amount 0) 1)
((or (< amount 0) (= kinds-of-coins 0)) 0)
(else (+ (cc amount
(- kinds-of-coins 1))
(cc (- amount
(first-denomination kinds-of-coins))
kinds-of-coins)))))
(define (first-denomination kinds-of-coins)
(cond ((= kinds-of-coins 1) 1)
((= kinds-of-coins 2) 5)
((= kinds-of-coins 3) 10)
((= kinds-of-coins 4) 25)
((= kinds-of-coins 5) 50)))

(display (count-change 100))
(newline)
\ --------------------- 8< ---------------------------

Groetjes Albert
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17616&group=comp.lang.lisp#17616

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: zbigniew2011@gmail.com (LIT)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 15:57:48 +0000
Organization: novaBBS
Message-ID: <7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1334526"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$RouHGNAStwwxT.VYxIZlg.dANyx/gf6BsLT5iv8TjRNGdyqS0HtkK
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 63063318607af797e83f549ce5239cf03fdbe117
 by: LIT - Wed, 27 Dec 2023 15:57 UTC

> Recursion is silly (if you can avoid it).

Not really.

Recursion is elegant — but actually isn't any
more efficient than iteration, and of course
it requires stack space.

And yes, most of the time (or always?) there
does exist iterative counterpart solution
--
Z.

Re: recursion is silly

<87h6k3eer1.fsf@yaxenu.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17617&group=comp.lang.lisp#17617

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Followup: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jshem@yaxenu.org (Julieta Shem)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Followup-To: comp.lang.lisp
Date: Wed, 27 Dec 2023 14:28:50 -0300
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <87h6k3eer1.fsf@yaxenu.org>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="a3465c654d0859077c511d2f6e80ca26";
logging-data="5821"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+X6mJb6NGdYNpxar0OD9+aJFXP1ijHT0A="
Cancel-Lock: sha1:BOu7+fRixOci+cxFXOvc8mmwdxg=
sha1:/34yv/PnLJ6CtRr0KxUJyO6hajA=
 by: Julieta Shem - Wed, 27 Dec 2023 17:28 UTC

albert@cherry.(none) (albert) writes:

> I recently draw attention to a toy lisp in Forth.
> The example program was a scheme program that counts
> how many changes in coins are possible for e.g. 100 dollarcent.

By the way, this is a well-known problem likely very well known by
everyone in this news group. The scheme-procedure you presented is from
section 1.2.2 in

Structure and Interpretation of Computer Programs
Gerald J. Sussman, Harold Abelson, Julie Sussman

> The lisp had to improved to be able to compile the program. The
> improved version can hardly manage an input of 100 and takes a
> noticable time, 200 and following fails.
>
> A more sane program fills in a table where each entry is derived
> from previous entries.

The scheme version you presented builds such a table --- it's called the
stack of execution. (Just another name for ``table''.)

Re: recursion is silly

<87a5pveeit.fsf@yaxenu.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17618&group=comp.lang.lisp#17618

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Followup: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jshem@yaxenu.org (Julieta Shem)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Followup-To: comp.lang.lisp
Date: Wed, 27 Dec 2023 14:33:46 -0300
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <87a5pveeit.fsf@yaxenu.org>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="a3465c654d0859077c511d2f6e80ca26";
logging-data="5821"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+X/wmQlAj59Xik6gmuOTfAeY6Sa6UqVSg="
Cancel-Lock: sha1:ycosmS5oYX+LsJSgtJMv/n4bk24=
sha1:NdzD1jbNKNyYg4jvWHTo9E7ilBQ=
 by: Julieta Shem - Wed, 27 Dec 2023 17:33 UTC

zbigniew2011@gmail.com (LIT) writes:

>> Recursion is silly (if you can avoid it).
>
> Not really.
>
> Recursion is elegant — but actually isn't any
> more efficient than iteration, and of course
> it requires stack space.
>
> And yes, most of the time (or always?) there
> does exist iterative counterpart solution

What do you mean iterative? For instance, Gerald Sussman and Harold
Abelson classify as `iterative' the procedure

(def (f n [acc 1])
(if (= n 1)
1
(f (dec n) (* acc n)))),

which is self-referential.

Re: recursion is silly

<87edf7a5ff.fsf@nightsong.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17619&group=comp.lang.lisp#17619

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!news.samoylyk.net!newsfeed.xs3.de!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 10:03:48 -0800
Organization: A noiseless patient Spider
Lines: 4
Message-ID: <87edf7a5ff.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="122857e3b9512454cf0fc141f2155dbd";
logging-data="20002"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19eQiA1sYgIRm2lrnZW/7Kw"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:R+4TtyZoqhYCgmSHnn7avd80ki4=
sha1:xM3PimUXO2OqbR0AlT4s1ksMdX8=
 by: Paul Rubin - Wed, 27 Dec 2023 18:03 UTC

albert@cherry.(none) (albert) writes:
> This is a kind of reversed dynamic programming.

That is usually called memoization.

Re: recursion is silly

<04bd89cc76e3703196041b6a53369461@news.novabbs.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17620&group=comp.lang.lisp#17620

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: zbigniew2011@gmail.com (LIT)
Newsgroups: comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 18:23:30 +0000
Organization: novaBBS
Message-ID: <04bd89cc76e3703196041b6a53369461@news.novabbs.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com> <87a5pveeit.fsf@yaxenu.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1347442"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Posting-User: 63063318607af797e83f549ce5239cf03fdbe117
X-Rslight-Site: $2y$10$uIwB5tRkS8x0RUH20EqyV.68898T22pIhKzY8fWWdZFDcw8D56mOW
X-Spam-Checker-Version: SpamAssassin 4.0.0
 by: LIT - Wed, 27 Dec 2023 18:23 UTC

> What do you mean iterative?

An iterative function is one that loops to
repeat some part of the code, and a recursive
function is one that calls itself again to
repeat the code
--
Z.

Re: recursion is silly

<d0544a1937a1c90ab6ccc2fabf297663@news.novabbs.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17621&group=comp.lang.lisp#17621

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: zbigniew2011@gmail.com (LIT)
Newsgroups: comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 18:29:39 +0000
Organization: novaBBS
Message-ID: <d0544a1937a1c90ab6ccc2fabf297663@news.novabbs.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com> <87a5pveeit.fsf@yaxenu.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1347794"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 63063318607af797e83f549ce5239cf03fdbe117
X-Rslight-Site: $2y$10$JPlnv4juwut1NV2iSWfNY.QnqJ8RgNnr7P4rXNLgCoAMUs5ZQRcHe
 by: LIT - Wed, 27 Dec 2023 18:29 UTC

> What do you mean iterative?

An iterative function is one that loops
to repeat some part of the code, and
a recursive function is one that calls
itself again to repeat the code
--
Z.

Re: recursion is silly

<169c6a0bc59c6af8cf62224ef319c5ee@news.novabbs.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17622&group=comp.lang.lisp#17622

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: minforth@gmx.net (minforth)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 18:43:55 +0000
Organization: novaBBS
Message-ID: <169c6a0bc59c6af8cf62224ef319c5ee@news.novabbs.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1349058"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$7bm10Xxgx4G7qy5eZjFYqeui19YWJ75XcQ8BOgvDkeeKttnlKmbva
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 0d6d33dbe0e2e1ff58b82acfc1a8a32ac3b1cb72
 by: minforth - Wed, 27 Dec 2023 18:43 UTC

none wrote:

> Recursion is silly (if you can avoid it).

> I recently draw attention to a toy lisp in Forth.
> The example program was a scheme program that counts
> how many changes in coins are possible for e.g. 100 dollarcent.
> The lisp had to improved to be able to compile the program.
> The improved version can hardly manage an input of 100 and takes
> a noticable time, 200 and following fails.

IOW educated brute force can beat elegant recursion.

You are right in a brutish

Re: recursion is silly

<87y1dfcskx.fsf@yaxenu.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17623&group=comp.lang.lisp#17623

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jshem@yaxenu.org (Julieta Shem)
Newsgroups: comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 17:13:02 -0300
Organization: A noiseless patient Spider
Lines: 11
Message-ID: <87y1dfcskx.fsf@yaxenu.org>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
<87a5pveeit.fsf@yaxenu.org>
<04bd89cc76e3703196041b6a53369461@news.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="a3465c654d0859077c511d2f6e80ca26";
logging-data="57206"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+fo0STUhjJfvWEa+SOWWumzNQbMG2w4VA="
Cancel-Lock: sha1:GZQRAAfT5i2prtsQ8WeSnMk9Hfw=
sha1:vV608LGHaTHF9uJUCEAM3/P1nOM=
 by: Julieta Shem - Wed, 27 Dec 2023 20:13 UTC

zbigniew2011@gmail.com (LIT) writes:

>> What do you mean iterative?
>
> An iterative function is one that loops to
> repeat some part of the code, and a recursive
> function is one that calls itself again to
> repeat the code

Such calling itself again is a form of going back to work on a similar
problem, which seems to fall under your definition of that which loops.

Re: recursion is silly

<87a5pv9z08.fsf@nightsong.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17624&group=comp.lang.lisp#17624

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 12:22:31 -0800
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <87a5pv9z08.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<87edf7a5ff.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="122857e3b9512454cf0fc141f2155dbd";
logging-data="58027"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+wugjpUYCDJhGyAXrazRyv"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:tQxF/Hi0K+/0d/Ro4UYiSAZtpPE=
sha1:529LB0Mtmm/eZZ0dzxStiFM5jvY=
 by: Paul Rubin - Wed, 27 Dec 2023 20:22 UTC

Paul Rubin <no.email@nospam.invalid> writes:
>> This is a kind of reversed dynamic programming.
> That is usually called memoization.

Added: in Scheme, it can be idiomatic to implement memoization using
force and delay. So for the coin problem you'd have a lookup table
indexed by the amount to be changed, and the sizes of coins available,
giving the number of ways to change that amount with those coins.

Each slot in the table would say something like (delay (cc amount coins))
where cc is a recursive function that would return a delayed sum of
the different table lookups corresponding to different breakdowns of
the amount. Finally, you would force the table entries for the amounts
1,2...100 or whatever.

I haven't tried this so I may be missing something, but the above
follows a common pattern.

Re: recursion is silly

<87mstvbcef.fsf@jmertens.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17625&group=comp.lang.lisp#17625

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!nntp.comgw.net!paganini.bofh.team!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!jmertens.eternal-september.org!.POSTED!not-for-mail
From: joerg-mertens@t-online.de (Joerg Mertens)
Newsgroups: comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 21:47:52 +0100
Organization: privat
Lines: 19
Message-ID: <87mstvbcef.fsf@jmertens.eternal-september.org>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
<87a5pveeit.fsf@yaxenu.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: jmertens.eternal-september.org; posting-host="1db2dd320bbddd5e33fe0d104fe6c684";
logging-data="66890"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jjBPb87XNuUsVAL+lPPqfk18HZRiyoKM="
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:GKTxd28oH+srb9JC0uoLXZTg86A=
sha1:BpbBgJNyBaVtMMZ0kjR9KzjkvRs=
 by: Joerg Mertens - Wed, 27 Dec 2023 20:47 UTC

Julieta Shem <jshem@yaxenu.org> writes:

> What do you mean iterative? For instance, Gerald Sussman and Harold
> Abelson classify as `iterative' the procedure
>
> (def (f n [acc 1])
> (if (= n 1)
> 1
> (f (dec n) (* acc n)))),
>
> which is self-referential.

Are you sure? IIRC they differentiate between programs and processes.
So your function would be a recursive program but it doesn't create a
recursive process (assumed the implementation is tail call optimized).

I admit that it is a long time since I read the book.

Regards

Re: recursion is silly

<8734vn9uit.fsf@nightsong.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17626&group=comp.lang.lisp#17626

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 13:59:22 -0800
Organization: A noiseless patient Spider
Lines: 57
Message-ID: <8734vn9uit.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<87edf7a5ff.fsf@nightsong.com> <87a5pv9z08.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="122857e3b9512454cf0fc141f2155dbd";
logging-data="85489"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Ft+DNBDpHCHN3vCkATk5p"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:0duqDH8KdIVGR6wT9VGQ2LzFvEw=
sha1:yvCZZrp9L2S8UKayfI0g1PWghXg=
 by: Paul Rubin - Wed, 27 Dec 2023 21:59 UTC

Paul Rubin <no.email@nospam.invalid> writes:
> I haven't tried this so I may be missing something, but the above
> follows a common pattern.

I still haven't worked out the details of the force/delay approach, but
here (at bottom) is a Scheme version that implements memoization using a
hash table by brute force. 100 cents and 200 cents take no perceptible
time.

I ran it with Guile 3.0 (compilation enabled) and got these results on a
small Hetzner VM (number of cents to be changed, result, cpu time in
seconds, and ram consumption in KB):

|---------+------------------------+-------+--------|
| amt | result | sec | KB |
|---------+------------------------+-------+--------|
| 100 | 293 | 0.01 | 9264 |
| 200 | 2728 | 0.01 | 9340 |
| 1000 | 2103596 | 0.01 | 10080 |
| 10000 | 139946140451 | 0.13 | 15176 |
| 100000 | 13398445413854501 | 1.25 | 67564 |
| 500000 | 41707305668679272501 | 5.76 | 293964 |
| 1000000 | 1333983445341383545001 | 15.24 | 580696 |
|---------+------------------------+-------+--------|

================================================================

(define (singleton? lst)
(and (not (null? lst))
(null? (cdr lst))))

(define allcoins '(1 5 10 25 50 100))

(define (cc amount coins)
(cond ((= amount 0) 1)
((< amount 0) 0)
((null? coins) 0)
((singleton? coins)
(if (= (remainder amount (car coins)) 0)
1
0))
(#t (+ (cc (- amount (car coins)) coins)
(cc amount (cdr coins))))))

(define (memoize f)
(define a (make-hash-table))
(define (g . args)
(let ((v (hash-ref a args)))
(if v v (hash-set! a args (apply f args)))))
g)

(set! cc (memoize cc))

(define amt (string->number (cadr (command-line))))

(display (cc amt allcoins))
(display "\n")

Re: recursion is silly

<feea51c7336d6888f6d7b73e76cacebd@news.novabbs.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17627&group=comp.lang.lisp#17627

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: mhx@iae.nl (mhx)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 22:29:43 +0000
Organization: novaBBS
Message-ID: <feea51c7336d6888f6d7b73e76cacebd@news.novabbs.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <87edf7a5ff.fsf@nightsong.com> <87a5pv9z08.fsf@nightsong.com> <8734vn9uit.fsf@nightsong.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1367257"; mail-complaints-to="usenet@i2pn2.org";
posting-account="t+lO0yBNO1zGxasPvGSZV1BRu71QKx+JE37DnW+83jQ";
User-Agent: Rocksolid Light
X-Rslight-Site: $2y$10$2EGctMEpBVX0jVf1JxcjSO6LrF.WYkJW5ja1Dti0qL3NbSVi94qm6
X-Spam-Checker-Version: SpamAssassin 4.0.0
X-Rslight-Posting-User: 463cbf1a76c808942982a163321348c75477c065
 by: mhx - Wed, 27 Dec 2023 22:29 UTC

> 1000000 | 1333983445341383545001 | 15.24 | 580696

That is quite different from the result none showed.
| time coinsusa 1,000,000
| 666793341266850001
| real 0m0.353s

I guess you are computing something different?
The time for 1,000,000 is also quite large, 15.24s vs 10ms (with iForth).

-marcel

Re: recursion is silly

<87y1df8e61.fsf@nightsong.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17628&group=comp.lang.lisp#17628

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 14:37:58 -0800
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <87y1df8e61.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<87edf7a5ff.fsf@nightsong.com> <87a5pv9z08.fsf@nightsong.com>
<8734vn9uit.fsf@nightsong.com>
<feea51c7336d6888f6d7b73e76cacebd@news.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="122857e3b9512454cf0fc141f2155dbd";
logging-data="95615"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18c9D4lHgZo9hAa0sOyCVtD"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:DsIxy0BtQr4yG6OE4gEax1HmkRU=
sha1:CDNSs9OvpuvV6B9t7aCAYLj3D/g=
 by: Paul Rubin - Wed, 27 Dec 2023 22:37 UTC

mhx@iae.nl (mhx) writes:
> I guess you are computing something different?
> The time for 1,000,000 is also quite large, 15.24s vs 10ms (with iForth).

Interesting, what values do you get for 100, 200, etc.? It could be
that my method is wrong. I just did it off the top of my head and may
have done something dumb.

Re: recursion is silly

<pu8poi13a1m0lq1am4a2c4fpop5nro1emh@4ax.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17629&group=comp.lang.lisp#17629

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!.POSTED!not-for-mail
From: gneuner2@comcast.net (George Neuner)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 17:44:39 -0500
Organization: i2pn2 (i2pn.org)
Message-ID: <pu8poi13a1m0lq1am4a2c4fpop5nro1emh@4ax.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: i2pn2.org;
logging-data="1368287"; mail-complaints-to="usenet@i2pn2.org";
posting-account="h5eMH71iFfocGZucc+SnA0y5I+72/ecoTCcIjMd3Uww";
User-Agent: ForteAgent/8.00.32.1272
X-Spam-Checker-Version: SpamAssassin 4.0.0
 by: George Neuner - Wed, 27 Dec 2023 22:44 UTC

On Wed, 27 Dec 2023 15:57:48 +0000, zbigniew2011@gmail.com (LIT)
wrote:

>> Recursion is silly (if you can avoid it).
>
>Not really.
>
>Recursion is elegant — but actually isn't any
>more efficient than iteration, and of course
>it requires stack space.

Not necessarily. Scheme compilers actually are *required* to turn
self recursion into a loop (except in the case of a supported "debug"
mode).

I think some of the Lisps can do it also - at least with
(optimize (debug 0) (space 3))

>And yes, most of the time (or always?) there
>does exist iterative counterpart solution

Always. Given enough resources (which could be code or memory space),
any recursion can be turned into an equivalent loop, and vice versa.

Re: recursion is silly

<87tto38djn.fsf@nightsong.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17630&group=comp.lang.lisp#17630

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 14:51:24 -0800
Organization: A noiseless patient Spider
Lines: 23
Message-ID: <87tto38djn.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<87edf7a5ff.fsf@nightsong.com> <87a5pv9z08.fsf@nightsong.com>
<8734vn9uit.fsf@nightsong.com>
<feea51c7336d6888f6d7b73e76cacebd@news.novabbs.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="122857e3b9512454cf0fc141f2155dbd";
logging-data="99471"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX183wUZQyr2apV7Oq0UlBA9u"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:ZGMz3HW9qbz9AhGwujNDr8pEqhY=
sha1:cRExC0/DvoYHbMaOhfQqWPlCUW4=
 by: Paul Rubin - Wed, 27 Dec 2023 22:51 UTC

mhx@iae.nl (mhx) writes:
> I guess you are computing something different?

Aha, one difference I notice is that Albert's code doesn't have a 100
cent coin. I included that because we have them in the US, though they
aren't used much except for in parking meters. They were introduced
partly for use in vending machines, but those now tend to have dollar
bill slots and sometimes also take credit cards.

Let me try again:

|---------+--------------------+-------+--------|
| 100 | 292 | 0.13 | 33644 |
| 200 | 2435 | 0.00 | 9412 |
| 1000 | 801451 | 0.01 | 9852 |
| 10000 | 6794128501 | 0.05 | 14316 |
| 100000 | 66793412685001 | 1.11 | 63904 |
| 500000 | 41682501983425001 | 6.07 | 240260 |
| 1000000 | 666793341266850001 | 10.67 | 475044 |
|---------+--------------------+-------+--------|

So we're getting the same results for 1000000 now, but my version is
still a lot slower. Maybe the memoization is not working right. Hmm.

Re: recursion is silly

<20231227150908.647@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17631&group=comp.lang.lisp#17631

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 433-929-6894@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 23:12:07 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <20231227150908.647@kylheku.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
<pu8poi13a1m0lq1am4a2c4fpop5nro1emh@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 27 Dec 2023 23:12:07 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="a83eb1c745b020314dcfd8bc886c80da";
logging-data="104050"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FjksMkqCEj3hrsxI2mukLGUKj0es7Ago="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:/ErjoAOrR1sXIb72msw8jcQdcoI=
 by: Kaz Kylheku - Wed, 27 Dec 2023 23:12 UTC

On 2023-12-27, George Neuner <gneuner2@comcast.net> wrote:
> On Wed, 27 Dec 2023 15:57:48 +0000, zbigniew2011@gmail.com (LIT)
> wrote:
>
>>> Recursion is silly (if you can avoid it).
>>
>>Not really.
>>
>>Recursion is elegant — but actually isn't any
>>more efficient than iteration, and of course
>>it requires stack space.
>
> Not necessarily. Scheme compilers actually are *required* to turn
> self recursion into a loop (except in the case of a supported "debug"
> mode).

Careful there: not all self-recursion is tail recursion!

For instance, the obvious way to write a recursive factorial fails
to be tail-recursive, but can be rewritten to tail from with
accumulator passing.

(I know you know all this, likely better than me, but `tis the
season to have your egg nogged.)

Re: recursion is silly

<nnd$719df295$3cd5929c@daceaef01f288931>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17632&group=comp.lang.lisp#17632

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com> <pu8poi13a1m0lq1am4a2c4fpop5nro1emh@4ax.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$719df295$3cd5929c@daceaef01f288931>
Organization: KPN B.V.
Date: Thu, 28 Dec 2023 00:37:16 +0100
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe005.abavia.com!abp001.abavia.com!news.kpn.nl!not-for-mail
Lines: 39
Injection-Date: Thu, 28 Dec 2023 00:37:16 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 2172
 by: none - Wed, 27 Dec 2023 23:37 UTC

In article <pu8poi13a1m0lq1am4a2c4fpop5nro1emh@4ax.com>,
George Neuner <gneuner2@comcast.net> wrote:
>On Wed, 27 Dec 2023 15:57:48 +0000, zbigniew2011@gmail.com (LIT)
>wrote:
>
>>> Recursion is silly (if you can avoid it).
>>
>>Not really.
>>
>>Recursion is elegant — but actually isn't any
>>more efficient than iteration, and of course
>>it requires stack space.
>
>Not necessarily. Scheme compilers actually are *required* to turn
>self recursion into a loop (except in the case of a supported "debug"
>mode).
>
>I think some of the Lisps can do it also - at least with
> (optimize (debug 0) (space 3))
>
>
>>And yes, most of the time (or always?) there
>>does exist iterative counterpart solution
>
>Always. Given enough resources (which could be code or memory space),
>any recursion can be turned into an equivalent loop, and vice versa.

In the Knuth TAOCP you cannot find recursive algorithms. They are
intended to run fast, and algorithms are specified with explicit
stacks. Of course the size of those stacks are specified.

Groetjes Albert
>
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<nnd$106cbdc8$69f96df4@5214b8941b584414>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17633&group=comp.lang.lisp#17633

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <87edf7a5ff.fsf@nightsong.com> <87a5pv9z08.fsf@nightsong.com> <8734vn9uit.fsf@nightsong.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$106cbdc8$69f96df4@5214b8941b584414>
Organization: KPN B.V.
Date: Thu, 28 Dec 2023 00:59:52 +0100
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!2.eu.feeder.erje.net!feeder.erje.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe004.abavia.com!abp001.abavia.com!news.kpn.nl!not-for-mail
Lines: 55
Injection-Date: Thu, 28 Dec 2023 00:59:52 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 2940
 by: none - Wed, 27 Dec 2023 23:59 UTC

In article <8734vn9uit.fsf@nightsong.com>,
Paul Rubin <no.email@nospam.invalid> wrote:
>Paul Rubin <no.email@nospam.invalid> writes:
>> I haven't tried this so I may be missing something, but the above
>> follows a common pattern.
>
>I still haven't worked out the details of the force/delay approach, but
>here (at bottom) is a Scheme version that implements memoization using a
>hash table by brute force. 100 cents and 200 cents take no perceptible
>time.
>
>I ran it with Guile 3.0 (compilation enabled) and got these results on a
>small Hetzner VM (number of cents to be changed, result, cpu time in
>seconds, and ram consumption in KB):
>
> |---------+------------------------+-------+--------|
> | amt | result | sec | KB |
> |---------+------------------------+-------+--------|
> | 100 | 293 | 0.01 | 9264 |
> | 200 | 2728 | 0.01 | 9340 |
> | 1000 | 2103596 | 0.01 | 10080 |
> | 10000 | 139946140451 | 0.13 | 15176 |
> | 100000 | 13398445413854501 | 1.25 | 67564 |
> | 500000 | 41707305668679272501 | 5.76 | 293964 |
> | 1000000 | 1333983445341383545001 | 15.24 | 580696 |
> |---------+------------------------+-------+--------|
>
>================================================================
>
<SNIP>

Impressed. My answer for 1000000 is actually wrong
because I didn't know that there are 1 dollar coins.

Improving my program:

~/euler: time coinsusa 100000
13398445413854501

real 0m0.095s
user 0m0.079s
sys 0m0.001s

Memory footprint estimated 900 Kbyte.

I see that you go beyond 64 bit numbers and I cannot
calculate the million dollarcent challenge.
The timing and foot print are slightly less as a reward
for more analysis.
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<nnd$51215f04$7697de00@a06ac5223ea6d9fe>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17634&group=comp.lang.lisp#17634

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <87a5pv9z08.fsf@nightsong.com> <8734vn9uit.fsf@nightsong.com> <feea51c7336d6888f6d7b73e76cacebd@news.novabbs.com>
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$51215f04$7697de00@a06ac5223ea6d9fe>
Organization: KPN B.V.
Date: Thu, 28 Dec 2023 01:07:10 +0100
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer03.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!feed.abavia.com!abe006.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 27
Injection-Date: Thu, 28 Dec 2023 01:07:10 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
X-Received-Bytes: 1793
 by: none - Thu, 28 Dec 2023 00:07 UTC

In article <feea51c7336d6888f6d7b73e76cacebd@news.novabbs.com>,
mhx <mhx@iae.nl> wrote:
>> 1000000 | 1333983445341383545001 | 15.24 | 580696
>
>That is quite different from the result none showed.
>| time coinsusa 1,000,000
>| 666793341266850001
>| real 0m0.353s
>
>I guess you are computing something different?
>The time for 1,000,000 is also quite large, 15.24s vs 10ms (with iForth).

Rubin is apparently knowledgeable about USA coins.
He corrected the coins table to contain 1 dollar coins. (What do I know.)
That spoils the benchmark effect.
Can you run the program again with 100 cent coins?
Maybe with 100,000 because now a million overflows 64 bit.

>
>-marcel
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<nnd$3d08c80e$1b85777b@7567d7d5aed93ab3>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17635&group=comp.lang.lisp#17635

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <8734vn9uit.fsf@nightsong.com> <feea51c7336d6888f6d7b73e76cacebd@news.novabbs.com> <87tto38djn.fsf@nightsong.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$3d08c80e$1b85777b@7567d7d5aed93ab3>
Organization: KPN B.V.
Date: Thu, 28 Dec 2023 01:26:22 +0100
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!tr2.iad1.usenetexpress.com!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!2001:67c:174:101:2:67:202:4.MISMATCH!feed.abavia.com!abe004.abavia.com!abp001.abavia.com!news.kpn.nl!not-for-mail
Lines: 46
Injection-Date: Thu, 28 Dec 2023 01:26:22 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
 by: none - Thu, 28 Dec 2023 00:26 UTC

In article <87tto38djn.fsf@nightsong.com>,
Paul Rubin <no.email@nospam.invalid> wrote:
>mhx@iae.nl (mhx) writes:
>> I guess you are computing something different?
>
>Aha, one difference I notice is that Albert's code doesn't have a 100
>cent coin. I included that because we have them in the US, though they
>aren't used much except for in parking meters. They were introduced
>partly for use in vending machines, but those now tend to have dollar
>bill slots and sometimes also take credit cards.
>
>Let me try again:
>
> |---------+--------------------+-------+--------|
> | 100 | 292 | 0.13 | 33644 |
> | 200 | 2435 | 0.00 | 9412 |
> | 1000 | 801451 | 0.01 | 9852 |
> | 10000 | 6794128501 | 0.05 | 14316 |
> | 100000 | 66793412685001 | 1.11 | 63904 |
> | 500000 | 41682501983425001 | 6.07 | 240260 |
> | 1000000 | 666793341266850001 | 10.67 | 475044 |
> |---------+--------------------+-------+--------|
>
>So we're getting the same results for 1000000 now, but my version is
>still a lot slower. Maybe the memoization is not working right. Hmm.

You must be kidding. The memoization using standard tools impressed me.
Compared to the result below it is only one order of magnitude.

In the meantime I have made a double precision version:

~/euler: time coinsusad 1000000
1333983445341383545001

real 0m1.209s
user 0m1.038s
sys 0m0.021s

A double precision version takes double the time,
900 ms for the original challenge.
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Re: recursion is silly

<umiink$3q9d$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17636&group=comp.lang.lisp#17636

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dxforth@gmail.com (dxf)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Thu, 28 Dec 2023 12:24:05 +1100
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <umiink$3q9d$1@dont-email.me>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
<pu8poi13a1m0lq1am4a2c4fpop5nro1emh@4ax.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 28 Dec 2023 01:24:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b41c9766ec23f8650ed4689fc7b6c8a1";
logging-data="125229"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1KCEVApL8HcM+grQNY+xB"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:kJ3SV1ZtAta2gnFf0ubyIBTYPNM=
In-Reply-To: <pu8poi13a1m0lq1am4a2c4fpop5nro1emh@4ax.com>
Content-Language: en-GB
 by: dxf - Thu, 28 Dec 2023 01:24 UTC

On 28/12/2023 9:44 am, George Neuner wrote:
> On Wed, 27 Dec 2023 15:57:48 +0000, zbigniew2011@gmail.com (LIT)
> wrote:
>
>>> Recursion is silly (if you can avoid it).
>>
>> Not really.
>>
>> Recursion is elegant — but actually isn't any
>> more efficient than iteration, and of course
>> it requires stack space.
>
> Not necessarily. Scheme compilers actually are *required* to turn
> self recursion into a loop (except in the case of a supported "debug"
> mode).
>
> I think some of the Lisps can do it also - at least with
> (optimize (debug 0) (space 3))

That's kind of offensive (to the programmer) which - not unlike ChatGPT -
puts humans in a diminished role. So the question becomes will humans have
any role at all? Much has been said about 'general purpose subroutines'.
ISTM the only logical reason to have them and (by implication) standards
is that they benefit automation.

Re: recursion is silly

<87sf3naxd5.fsf@yaxenu.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17637&group=comp.lang.lisp#17637

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jshem@yaxenu.org (Julieta Shem)
Newsgroups: comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 23:12:38 -0300
Organization: A noiseless patient Spider
Lines: 96
Message-ID: <87sf3naxd5.fsf@yaxenu.org>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<7443e54dc76553fbe54bdb46baa721bf@news.novabbs.com>
<87a5pveeit.fsf@yaxenu.org>
<87mstvbcef.fsf@jmertens.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="0f55de256f731b22a5fb9b4cc890dfdd";
logging-data="144483"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/fSNzK/AgMn1Lm1E1cAHp33xHU5i/SdeQ="
Cancel-Lock: sha1:UWEbHBU3zPwGihJzlWZM1HwkM8M=
sha1:rifCbveENFNXL2ALEakbzYmbdGs=
 by: Julieta Shem - Thu, 28 Dec 2023 02:12 UTC

Joerg Mertens <joerg-mertens@t-online.de> writes:

> Julieta Shem <jshem@yaxenu.org> writes:
>
>> What do you mean iterative? For instance, Gerald Sussman and Harold
>> Abelson classify as `iterative' the procedure
>>
>> (def (f n [acc 1])
>> (if (= n 1)
>> 1
>> (f (dec n) (* acc n)))),
>>
>> which is self-referential.
>
> Are you sure? IIRC they differentiate between programs and processes.

They do --- between ``procedures'' and processes.

> So your function would be a recursive program but it doesn't create a
> recursive process (assumed the implementation is tail call optimized).

On section 1.2.1, Figure 1.4 is roughly

--8<---------------cut here---------------start------------->8---
(factorial 6)
(fact-iter 1 1 6)
(fact-iter 1 2 6)
(fact-iter 2 3 6)
(fact-iter 6 4 6)
(fact-iter 24 5 6)
(fact-iter 120 6 6)
(fact-iter 720 7 6)
720

Figure 1.4: A linear iterative process for computing 6!.
--8<---------------cut here---------------end--------------->8---

You're right that they don't call it an iterative /procedure/, but they
do call it an iterative /process/. So maybe I can fix my post with

--8<---------------cut here---------------start------------->8---
[They] classify as `iterative' the process generated by the procedure

(def (f n [acc 1])
(if (= n 1)
1
(f (dec n) (* acc n)))),

which is self-referential.
--8<---------------cut here---------------end--------------->8---

Here's their words directly:

--8<---------------cut here---------------start------------->8---
Consider the [linearly recursive naive factorial procedure]. The
substitution model reveals a shape of expansion followed by contraction,
indicated by the arrow in figure 1.3. The expansion occurs as the
process builds up a chain of deferred operations (in this case, a chain
of multiplications). The contraction occurs as the operations are
actually performed. This type of process, characterized by a chain of
deferred operations, is called a recursive process. Carrying out this
process requires that the interpreter keep track of the operations to be
performed later on. In the computation of n!, the length of the chain of
deferred multiplications, and hence the amount of information needed to
keep track of it, grows linearly with n (is proportional to n), just
like the number of steps. Such a process is called a linear recursive
process.

By contrast, the second process [the fact-iter procedure of Figure 1.4]
does not grow and shrink. At each step, all we need to keep track of,
for any n, are the current values of the variables product, counter, and
max-count. We call this an /iterative process/. In general, an iterative
process is one whose state can be summarized by a fixed number of state
variables, together with a fixed rule that describes how the state
variables should be updated as the process moves from state to state and
an (optional) end test that specifies conditions under which the process
should terminate. In computing n!, the number of steps required grows
linearly with n. Such a process is called a linear iterative process.
--8<---------------cut here---------------end--------------->8---

They constrast the two notions:

--8<---------------cut here---------------start------------->8---
In contrasting iteration and recursion, we must be careful not to
confuse the notion of a recursive process with the notion of a recursive
procedure. When we describe a procedure as recursive, we are referring
to the syntactic fact that the procedure definition refers (either
directly or indirectly) to the procedure itself. But when we describe a
process as following a pattern that is, say, linearly recursive, we are
speaking about how the process evolves, not about the syntax of how a
procedure is written. It may seem disturbing that we refer to a
recursive procedure such as fact-iter as generating an iterative
process. However, the process really is iterative: Its state is captured
completely by its three state variables, and an interpreter need keep
track of only three variables in order to execute the process.
--8<---------------cut here---------------end--------------->8---

Re: recursion is silly

<87plyq9419.fsf@nightsong.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17638&group=comp.lang.lisp#17638

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.forth,comp.lang.lisp
Subject: Re: recursion is silly
Date: Wed, 27 Dec 2023 23:31:30 -0800
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <87plyq9419.fsf@nightsong.com>
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c>
<8734vn9uit.fsf@nightsong.com>
<feea51c7336d6888f6d7b73e76cacebd@news.novabbs.com>
<87tto38djn.fsf@nightsong.com>
<nnd$3d08c80e$1b85777b@7567d7d5aed93ab3>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="e0d3f9bff17cd65fc8c4765f2db43fdb";
logging-data="335870"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+f9S5OA3bndEyvMdW9Fb1K"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:JB+GGeqOUiVRwwbD1R6doIl29UY=
sha1:cEU6yGMQAYhlYoaG32gAfWnvfn4=
 by: Paul Rubin - Thu, 28 Dec 2023 07:31 UTC

albert@cherry.(none) (albert) writes:
> You must be kidding. The memoization using standard tools impressed me.
> Compared to the result below it is only one order of magnitude.

I see, so the algorithms are basically similar. I haven't examined your
code closely yet. But I see you are using a much more efficient memo
table than I am. Yours is indexed on the offset into the coin list,
while mine uses the entire list. I might try adapting mine, maybe to
use arrays instead of a hash table.

I noticed I got an 8x speedup and 5x memory reduction by simply
reversing the order of the coin list, i.e. using (100 50 25 10 5 1)
instead of (1 5 10 25 50 100).

In reality, 50 cent coins and dollar coins are rarely used in the US.
50 cent coins are large enough to be cumbersome, and dollar coins never
became popular because they are too easily confused with quarters (they
are just slightly larger).

Canada has all the coins that the US has, plus a 2 dollar coin, and they
actually use them. France before the Euro had even more, I believe.

Re: recursion is silly

<nnd$2c79d9a9$3069ce88@138cdb80d1146ce2>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=17639&group=comp.lang.lisp#17639

  copy link   Newsgroups: comp.lang.forth comp.lang.lisp
Newsgroups: comp.lang.forth,comp.lang.lisp
References: <nnd$6660ae2b$78615640@f2c7e8d01681877c> <87tto38djn.fsf@nightsong.com> <nnd$3d08c80e$1b85777b@7567d7d5aed93ab3> <87plyq9419.fsf@nightsong.com>
Subject: Re: recursion is silly
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
From: albert@cherry (none)
Originator: albert@cherry.(none) (albert)
Message-ID: <nnd$2c79d9a9$3069ce88@138cdb80d1146ce2>
Organization: KPN B.V.
Date: Thu, 28 Dec 2023 11:20:36 +0100
Path: i2pn2.org!i2pn.org!news.1d4.us!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!tr3.iad1.usenetexpress.com!feeder.usenetexpress.com!tr1.eu1.usenetexpress.com!2001:67c:174:101:1:67:202:5.MISMATCH!feed.abavia.com!abe005.abavia.com!abp002.abavia.com!news.kpn.nl!not-for-mail
Lines: 50
Injection-Date: Thu, 28 Dec 2023 11:20:36 +0100
Injection-Info: news.kpn.nl; mail-complaints-to="abuse@kpn.com"
 by: none - Thu, 28 Dec 2023 10:20 UTC

In article <87plyq9419.fsf@nightsong.com>,
Paul Rubin <no.email@nospam.invalid> wrote:
>albert@cherry.(none) (albert) writes:
>> You must be kidding. The memoization using standard tools impressed me.
>> Compared to the result below it is only one order of magnitude.
>
>I see, so the algorithms are basically similar. I haven't examined your
>code closely yet. But I see you are using a much more efficient memo
>table than I am. Yours is indexed on the offset into the coin list,
>while mine uses the entire list. I might try adapting mine, maybe to
>use arrays instead of a hash table.

It is similar in the sense that at the end intermediate results
are calculated in the same partial sums. There is a great difference
that it is reversed. Memoization asks for the intermediate results e.g. 100,5
to be calculated. I start from the bottom and just guess that all
intermediate results are needed.I start with calculating 1,1 2,1 3,1 ..
This depend on analysing the problem.
You will get nowhere by this method if you apply at eulerproject
problem 502 (counting castles).

>
>I noticed I got an 8x speedup and 5x memory reduction by simply
>reversing the order of the coin list, i.e. using (100 50 25 10 5 1)
>instead of (1 5 10 25 50 100).
>
>In reality, 50 cent coins and dollar coins are rarely used in the US.
>50 cent coins are large enough to be cumbersome, and dollar coins never
>became popular because they are too easily confused with quarters (they
>are just slightly larger).
>
>Canada has all the coins that the US has, plus a 2 dollar coin, and they
>actually use them. France before the Euro had even more, I believe.

France used denominations 10 centimes up till 10 Franc .
The 10 Franc piece and 2 euro piece and the Can 2 dollar piece
have comparable purchasing power. A the time the Netherlands
had a 2.5 guilder coins, also comparable.
Formally the Euro has a regular 1 2 5 10 20 50 100 200 series
but the 1 and 2 cent pieces are no more used in the Netherlands.
So 5 or 6 coins in actual use.

Groetjes Albert
>
--
Don't praise the day before the evening. One swallow doesn't make spring.
You must not say "hey" before you have crossed the bridge. Don't sell the
hide of the bear until you shot it. Better one bird in the hand than ten in
the air. First gain is a cat spinning. - the Wise from Antrim -

Pages:1234
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor