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: How to cast an imperative loop into a readable recursive function ? Date: Thu, 2 Dec 2010 17:50:43 -0800 (PST) Organization: http://groups.google.com Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: dough.gmane.org 1291949360 20359 80.91.229.12 (10 Dec 2010 02:49:20 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 10 Dec 2010 02:49:20 +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 03:49:16 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 1PQt2q-0001yU-1r for geh-help-gnu-emacs@m.gmane.org; Fri, 10 Dec 2010 03:49:16 +0100 Original-Received: from localhost ([127.0.0.1]:60295 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQt2p-000245-G2 for geh-help-gnu-emacs@m.gmane.org; Thu, 09 Dec 2010 21:49:15 -0500 Original-Path: usenet.stanford.edu!postnews.google.com!e20g2000vbn.googlegroups.com!not-for-mail Original-Newsgroups: comp.lang.lisp,comp.emacs,gnu.emacs.help Original-Lines: 61 Original-NNTP-Posting-Host: 75.31.79.167 Original-X-Trace: posting.google.com 1291341043 3970 127.0.0.1 (3 Dec 2010 01:50:43 GMT) Original-X-Complaints-To: groups-abuse@google.com Original-NNTP-Posting-Date: Fri, 3 Dec 2010 01:50:43 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: e20g2000vbn.googlegroups.com; posting-host=75.31.79.167; 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:296031 comp.emacs:100882 gnu.emacs.help:182786 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:77226 Archived-At: Background : This is strictly a style question about a solved problem. I wrote pseudocode in {} and converted to () and it worked. The line count of both pseudocodes was similar. regexp part was the one left as pseudocode in both. casting was also needed of string-to-integer. How to cast an imperative loop into a readable recursive function ? What questions to ask to guide thinking to it ? Here is the concrete problem. which is to find the nth character T_n in a string with erratic index reset and length of substring given. ie The string is preceeded by a value of start index s and length l (s=4,l=16)ab5csoikowlfg02c(s=39,l=23)rli3ubxvadf36ut8uguhijh(...)...### There can be gaps and the value of n is reset and usually greater but no need to make the assumption and the first one with the desired index is acceptable. ### denotes the end of the sequence. It is not guaranteed so a (message) should be echoed in the situation of error. I have written this as a loop and an (if test then else) inside it. Readablility is acceptable, however, I look for a lispy recursive style. (let ((char_skips (- n s) )) (while (not (= char_skips 0)) (progn (if (<= char_skips (+ l -1) ) (progn (forward-char char_skips) (setq char_skips 0)) (progn (forward-char l) (forward-search-regexp "(s=###l=###)" nil nil nil) ; put in an if and else block to test if FAIL (setq s (match-string 0)) (setq l (match-string 0)) (setq char_skips (- n s) ) ) ) ) ) )