[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to cast an imperative loop into a readable recursive function ?
From: |
Tim Bradshaw |
Subject: |
Re: How to cast an imperative loop into a readable recursive function ? |
Date: |
Fri, 3 Dec 2010 09:44:47 +0000 |
User-agent: |
Unison/2.1.3 |
On 2010-12-03 03:19:43 +0000, Katalin Sinkov said:
your iterate is again a loop, ie a non-functional construct
Is it? Consider this form:
(for (i 0 100)
(print i))
This is iterative, right, I mean obviously.
Except here's the definition of FOR:
(defmacro for ((var min max) &body decls-and-forms)
(let ((maxn (make-symbol "MAX"))
(nextn (make-symbol "NEXT-1")))
`(let ((,maxn ,max))
(labels ((,nextn (,var)
(when (= ,var ,maxn)
(return-from ,nextn ,maxn))
(let ((,var ,var))
,@decls-and-forms)
(,nextn (1+ ,var)))
(next ()
(,nextn (1+ ,var))))
(,nextn ,min)))))
There seems to be no iteration there, and expanding (and renaming
gensyms to make it clearer):
(let ((max 100))
(labels ((next-1 (i)
(when (= i max) (return-from next-1 max))
(let ((i i))
(print i))
(next-1 (1+ i)))
(next () (next-1 (1+ i))))
(next-1 0)))
No iteration there.
Re: How to cast an imperative loop into a readable recursive function ?, RG, 2010/12/09