all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to cast an imperative loop into a readable recursive function ?
@ 2010-12-03  1:50 Katalin Sinkov
  2010-12-03  2:17 ` Paul Rubin
  2010-12-03  2:48 ` RG
  0 siblings, 2 replies; 9+ messages in thread
From: Katalin Sinkov @ 2010-12-03  1:50 UTC (permalink / raw)
  To: help-gnu-emacs

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) )
	  )
	)
      )
    )
  )




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2010-12-03  9:44 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-12-03  1:50 How to cast an imperative loop into a readable recursive function ? Katalin Sinkov
2010-12-03  2:17 ` Paul Rubin
     [not found]   ` <8a5ef1e1-aab3-47bd-80e3-081f8dc65b0e@c39g2000yqi.googlegroups.com>
     [not found]     ` <84a24d31-6379-4b81-ac65-b0d8642ab7da@37g2000prx.googlegroups.com>
2010-12-03  3:30       ` Paul Rubin
     [not found]         ` <0c1c00c1-61ef-42be-a2d3-db1ff31035e8@29g2000yqq.googlegroups.com>
2010-12-03  7:29           ` Paul Rubin
2010-12-03  9:44       ` Tim Bradshaw
2010-12-03  2:48 ` RG
2010-12-03  3:12   ` Katalin Sinkov
     [not found]     ` <7xk4jrmtmg.fsf@ruckus.brouhaha.com>
2010-12-03  3:22       ` Katalin Sinkov
     [not found]     ` <87ipzbpgft.fsf@kuiper.lan.informatimago.com>
2010-12-03  7:37       ` Katalin Sinkov

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.