Skip to main content
added 1641 characters in body
Source Link
NickD
  • 36k
  • 4
  • 33
  • 50

You can use &rest to get all arguments in a list and then use dolist to loop over each element of the list individually e.g.:

(defun decimalizing-all-integers-no-matter-how-many (&rest args) (dolist (arg args) (insert (number-to-string (decimalizer arg))) (newline))) 
 (defun decimalizing-all-integers-no-matter-how-many (&rest args) (dolist (arg args) (insert (number-to-string (decimalizer arg))) (newline))) 

Refs:

  1. dolist
  2. Argument lists

Two additional points:

  1. You can use a mapping function instead of an explicit loop (as you guessed). Something like
(defun decimalizing-all-integers-no-matter-how-many (&rest args) (mapc (lambda (arg) (insert (number-to-string (decimalizer arg)))) args)) 

Mapping functions like mapcar and mapc loop implicitly over the elements of the list that is their second argument and apply to each element the function that is specified as the first argument. mapcar accumulates the results in a list which it returns; mapc is used for side-effects only (here, inserting a string into the buffer) and does not return any useful value.

  • The decimalizer function should probably return a string instead of a number: that would avoid the number-to-string call in the parent function. I would write it as follows:
(defun decimalizer (x) "Return a string representation of the argument (which must be a number or a string representation of a number) with one digit after the decimal point." (let ((y (cond ((stringp x) (string-to-number x)) ((numberp x) x) (t (user-error "Argument must be number or string: %S" x))))) (format "%.1f" y))) 

so we convert whatever we get into a number (if possible), convert that to a string using format and return the string. This would round the number to the nearest 10th which may or may not be what you want. But it is simpler than converting to a string and then doing regexp matching, which is generally fraught with dangers in numerical computing and should be avoided.

You can use &rest to get all arguments in a list and then use dolist to loop over each element of the list individually e.g.:

(defun decimalizing-all-integers-no-matter-how-many (&rest args) (dolist (arg args) (insert (number-to-string (decimalizer arg))) (newline))) 

Refs:

  1. dolist
  2. Argument lists

You can use &rest to get all arguments in a list and then use dolist to loop over each element of the list individually e.g.:

 (defun decimalizing-all-integers-no-matter-how-many (&rest args) (dolist (arg args) (insert (number-to-string (decimalizer arg))) (newline))) 

Refs:

  1. dolist
  2. Argument lists

Two additional points:

  1. You can use a mapping function instead of an explicit loop (as you guessed). Something like
(defun decimalizing-all-integers-no-matter-how-many (&rest args) (mapc (lambda (arg) (insert (number-to-string (decimalizer arg)))) args)) 

Mapping functions like mapcar and mapc loop implicitly over the elements of the list that is their second argument and apply to each element the function that is specified as the first argument. mapcar accumulates the results in a list which it returns; mapc is used for side-effects only (here, inserting a string into the buffer) and does not return any useful value.

  • The decimalizer function should probably return a string instead of a number: that would avoid the number-to-string call in the parent function. I would write it as follows:
(defun decimalizer (x) "Return a string representation of the argument (which must be a number or a string representation of a number) with one digit after the decimal point." (let ((y (cond ((stringp x) (string-to-number x)) ((numberp x) x) (t (user-error "Argument must be number or string: %S" x))))) (format "%.1f" y))) 

so we convert whatever we get into a number (if possible), convert that to a string using format and return the string. This would round the number to the nearest 10th which may or may not be what you want. But it is simpler than converting to a string and then doing regexp matching, which is generally fraught with dangers in numerical computing and should be avoided.

added 208 characters in body
Source Link
heemayl
  • 306
  • 2
  • 6

You can use &rest to get all arguments in a list and then use dolist to loop over each element of the list individually e.g.:

(defun decimalizing-all-integers-no-matter-how-many (&rest args) (dolist (arg args) (insert (number-to-string (decimalizer arg))) (newline))) 

Refs:

  1. dolist
  2. Argument lists

You can use &rest to get all arguments in a list and then use dolist to loop over each element of the list individually e.g.:

(defun decimalizing-all-integers-no-matter-how-many (&rest args) (dolist (arg args) (insert (number-to-string (decimalizer arg))) (newline))) 

You can use &rest to get all arguments in a list and then use dolist to loop over each element of the list individually e.g.:

(defun decimalizing-all-integers-no-matter-how-many (&rest args) (dolist (arg args) (insert (number-to-string (decimalizer arg))) (newline))) 

Refs:

  1. dolist
  2. Argument lists
Source Link
heemayl
  • 306
  • 2
  • 6

You can use &rest to get all arguments in a list and then use dolist to loop over each element of the list individually e.g.:

(defun decimalizing-all-integers-no-matter-how-many (&rest args) (dolist (arg args) (insert (number-to-string (decimalizer arg))) (newline)))