From a0b0756a7197bbf8984226d49e341494217c5913 Mon Sep 17 00:00:00 2001 From: "Richard M. Stallman" Date: Fri, 21 Jan 2000 02:08:58 +0000 Subject: [PATCH] (dolist, dotimes): Moved from cl-macs.el. --- lisp/subr.el | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lisp/subr.el b/lisp/subr.el index 683bc247941..ed754619174 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -78,6 +78,32 @@ change the list." (put 'unless 'lisp-indent-function 1) (put 'unless 'edebug-form-spec '(&rest form)) +(defmacro dolist (spec &rest body) + "(dolist (VAR LIST [RESULT]) BODY...): loop over a list. +Evaluate BODY with VAR bound to each car from LIST, in turn. +Then evaluate RESULT to get return value, default nil." + (let ((temp (gensym "--dolist-temp--"))) + (list 'block nil + (list* 'let (list (list temp (nth 1 spec)) (car spec)) + (list* 'while temp (list 'setq (car spec) (list 'car temp)) + (append body (list (list 'setq temp + (list 'cdr temp))))) + (if (cdr (cdr spec)) + (cons (list 'setq (car spec) nil) (cdr (cdr spec))) + '(nil)))))) + +(defmacro dotimes (spec &rest body) + "(dotimes (VAR COUNT [RESULT]) BODY...): loop a certain number of times. +Evaluate BODY with VAR bound to successive integers running from 0, +inclusive, to COUNT, exclusive. Then evaluate RESULT to get +the return value (nil if RESULT is omitted)." + (let ((temp (gensym "--dotimes-temp--"))) + (list 'block nil + (list* 'let (list (list temp (nth 1 spec)) (list (car spec) 0)) + (list* 'while (list '< (car spec) temp) + (append body (list (list 'incf (car spec))))) + (or (cdr (cdr spec)) '(nil)))))) + (defsubst caar (x) "Return the car of the car of X." (car (car x))) -- 2.39.5