help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to cast an imperative loop into a readable recursive function ?


From: Katalin Sinkov
Subject: How to cast an imperative loop into a readable recursive function ?
Date: Thu, 2 Dec 2010 17:50:43 -0800 (PST)
User-agent: G2/1.0

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]