The dolist
can have no effect on definitions
. If you remove it from your function you will see that you get a result (but not exactly what you wanted). The mapconcat
is evaluated and does not return nil
.
The result of (setq definitions (split-string (match-string 1 dictentry) "/" t))
in your test example is ("(n,adj-no) occupation" "business")
.
The mapconcat
applied to that would give "(n,adj-no) occupation\nbusiness"
dolist
returns nil
, so the mapconcat
is never evaluated.
If you wrap your dolist
with (progn ... t)
then the mapconcat
will be evaluated.
But your main problem is thinking that dolist
modifies the original list. As explained in the post you cited in your other question, you can use dolist
to modify individual elements of the list but not the list itself.
For example, if the list had particular strings or lists as its elements, you could modify those individual objects. (The individual elements of the list must of course be modifiable, for you to modify them. You cannot modify numbers, for example.)
But in your case you want to modify the list itself. You want to replace a given element with a completely different value -- that is, replace it in the list. That's not the same as modifying an element (which changes the value globally). You cannot do that with dolist
.
Here's one quick, ugly-hacky way to do what you want. It doesn't change your code much. It just replaces the dolist
by some code that modifies definitions
.
(defun japanese-get-definition (dictentry)
"Get a definition from a dictionary entry."
(let* ((index 1)
(definitions))
(save-match-data
(and (string-match "/\\(.*?\\)$" dictentry)
(setq definitions (split-string (match-string 1 dictentry) "/" t))
(let ((defs definitions))
(while defs
(setcar defs (concat (int-to-string index) ". " (car defs)))
(setq index (1+ index)
defs (cdr defs)))
t)
(mapconcat 'identity definitions "\n")))))
This is slightly simpler, but still not very pretty:
(defun japanese-get-definition (dictentry)
"Get a definition from a dictionary entry."
(let* ((index 1)
(definitions ()))
(save-match-data
(when (string-match "/\\(.*?\\)$" dictentry)
(setq definitions (split-string (match-string 1 dictentry) "/" t))
(let ((defs definitions))
(while defs
(setcar defs (concat (int-to-string index) ". " (car defs)))
(setq index (1+ index)
defs (cdr defs))))
(mapconcat 'identity definitions "\n")))))