1

I'm trying to figure out why my emacs keeps freezing when running this set of functions to split a string into a list of pairs. Is there some significant overhead happening when using string-split? Is there a different way to approach this problem?

(setq input "399822 801471
854475 776464
118051 369789
68399 518004
575096 332007
138222 180701
763798 380259
716159 578228
582494 519472
789135 852518
638621 681440")

(defun list-pairs (list)
  (when list
    (cons (split-string (car list) " " t) (list-pairs (cdr list)))))

(list-pairs (split-string input "\n" t))

I run this function and my emacs just freezes. I realize the same thing can be accomplished by using mapcar

(mapcar (lambda (x) (split-string x " " t)) (split-string input "\n" t))

(("399822" "801471") ("854475" "776464") 
 ("118051" "369789") ("68399" "518004") 
 ("575096" "332007") ("138222" "180701") 
 ("763798" "380259") ("716159" "578228") 
 ("582494" "519472") ("789135" "852518") 
 ("638621" "681440"))
  • 1
    Your first example with function list-pairs runs as expected for me in Emacs 26.3. No freezing. AFAIK Emacs does not have tail recursion optimization. So your second version is probably better. Note, that the linked SE-entry is quite old. I do not know the current status. They implemented lexical scoping meanwhile. So maybe, there is improvement. – Tobias Mar 25 at 5:07
  • I think this comment of Stefan is most interesting in the aspect whether tail recursion will be implemented in the future. – Tobias Mar 25 at 5:12
  • Noteworthy is that a degree of TCO has been implemented for the gccemacs project. – phils Mar 25 at 6:13
  • 1
    dylanjm, try your code under emacs -Q – phils Mar 25 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.