emacs - How to change the order of function body execution? -


i studying emacs-lisp following introduction.

i can understand below defun print out list in left-to-right order because print command comes before recursion (as understood):

  (defun print-elements-recursively (list)    "print each element of list on line of own.  uses recursion."    (when list                            ; do-again-test          (print (car list))              ; body          (print-elements-recursively     ; recursive call           (cdr list))))                  ; next-step-expression 

e.g. list of '(gazelle giraffe lion tiger). print order gazelle, giraffe, lion, tiger.

however, not understand why same order still holds when switch position of 2 expression within when body:

(defun print-elements-recursively (list)    "print each element of list on line of own.  uses recursion."    (when list                            ; do-again-test                        ; body          (print-elements-recursively     ; recursive call           (cdr list))          (print (car list))))                  ; next-step-expression 

per expectation, recursion happens before print function, therefore, order should reversed. may know why?

you did not evaluate second defun after defining it, , why items of input list still being printed in original order. adding second function same name global namespace not mean definition of first function automatically overwritten.

i suggest you

  • rename 1 of defuns
  • evaluate them both
  • and call each 1 of them separately.

the behavior should not persist when that.


aside printing elements of list in different order, note original function returns nil , second function returns printed representation of last (non-nil) item of input list. because (when list) returns nil , last expression gets evaluated when base case reached in first function. in second function invocations of print evaluated after base case reached.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -