From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Katalin Sinkov Newsgroups: gmane.emacs.help Subject: Re: How to cast an imperative loop into a readable recursive function ? Date: Thu, 2 Dec 2010 19:12:38 -0800 (PST) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1291956104 10473 80.91.229.12 (10 Dec 2010 04:41:44 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 10 Dec 2010 04:41:44 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Dec 10 05:41:38 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PQunZ-0001h3-VI for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Dec 2010 05:41:38 +0100 Original-Received: from localhost ([127.0.0.1]:32940 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQucb-0006Wb-38 for geh-help-gnu-emacs@m.gmane.org; Thu, 09 Dec 2010 23:30:17 -0500 Original-Path: usenet.stanford.edu!postnews.google.com!s5g2000yqm.googlegroups.com!not-for-mail Original-Newsgroups: comp.lang.lisp,comp.emacs,gnu.emacs.help Original-Lines: 49 Original-NNTP-Posting-Host: 75.31.67.218 Original-X-Trace: posting.google.com 1291345958 19455 127.0.0.1 (3 Dec 2010 03:12:38 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 3 Dec 2010 03:12:38 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: s5g2000yqm.googlegroups.com; posting-host=75.31.67.218; posting-account=q8u0hAoAAAA7uZRsDFj29PQdwm_l8nyT User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6,gzip(gfe) Original-Xref: usenet.stanford.edu comp.lang.lisp:296040 comp.emacs:100886 gnu.emacs.help:182791 X-Mailman-Approved-At: Thu, 09 Dec 2010 20:08:58 -0500 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:77335 Archived-At: On Dec 2, 6:48=A0pm, RG wrote: > In article > , > =A0Katalin Sinkov wrote: > > > How to cast an imperative loop into a readable recursive function ? > > What is a readable function? for a beginner like me, i was able to read those in McCarthy's paper, beautifully written recursive functions. > Ignoring that part, you can always rewrite something like: > > (while condition ...) > > as > > (iterate loop () > =A0 (when condition ... (loop))) > > where iterate is defined as: > > (defmacro iterate (name args &rest body) > =A0 `(labels ((,name ,(mapcar 'first args) ,@body)) > =A0 =A0 =A0(,name ,@(mapcar 'second args)))) > > Is that what you meant? > I am sorry that this is not what I have in mind. I am thinking of how McCarthy wrote all those difficult concepts nicely as recursive functions. He dissected a problem ingeniously to not have to iterate. In my case, I have a pattern that is often interrupted by a reset of the index. It is trivial to go to nth character in a string because the indices are contiguous. However, in this case, the indices are often reset, but mercifully, although not necessary, the length of the contiguous index sequence is also given so we dont have to look for beginning paren at every step. Take a look at my lisp code. it can be modified simply to run it and it runs in an emacs "ide".