[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Storing strings in a list when some condition is true
From: |
Heime |
Subject: |
Storing strings in a list when some condition is true |
Date: |
Sat, 29 Jul 2023 17:40:42 +0000 |
I am using this function to generate permutations of characters in a string.
The algorithm uses recursion, but the final string that is needed is the one
at which the condition (= i length) holds true (currently I just call message).
Suppose one starts with (permute "word" 0 4), different strings are generated
but the ones I need saved are the ones for which (= i length) is satisfied.
How can put all the words generated in a list ?
(defun permute (string i length)
"Generate permutations of STRING."
(if (= i length)
(message "%s" string)
(let ( (j i) )
(while (< j length)
(swap-chars string i j)
(permute string (1+ i) length)
(swap-chars string i j)
(setq j (1+ j))) )))
where swap-chars is
(defun swap-chars (string p q)
"Swap chars in STRING at positions P and Q.
This is a destructive operation."
(aset string p (prog1 (aref string q)
(aset string q (aref string p))))
string)
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Storing strings in a list when some condition is true,
Heime <=