help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Check for redundancy


From: Emanuel Berg
Subject: Re: Check for redundancy
Date: Sun, 28 Jun 2015 04:40:17 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

Emanuel Berg <embe8573@student.uu.se> writes:

> I just got and idea - stay tuned...

(defun make-backrefs (total)
  (if (> total 0)
      (format "%s\\\\%s" (make-backrefs (1- total)) total)
    "") )

(defun make-backrefs-while (total)
  (let ((refs ""))
    (while (> total 0)
      (setq refs (format "\\\\%s%s" total refs))
      (cl-decf total) )
    refs) )

(defun make-backrefs-tail (total &optional refs)
  (unless refs (setq refs ""))
  (if (> total 0)
      (make-backrefs-tail (1- total) (format "\\\\%s%s" total refs))
    refs) )

;; test they produce the same output at least :)
(let ((num-refs 5))
  (insert "\n\n")
  (insert ";; " (make-backrefs num-refs)       "\n")
  (insert ";; " (make-backrefs-while num-refs) "\n")
  (insert ";; " (make-backrefs-tail num-refs)  "\n") )

;; \\1\\2\\3\\4\\5
;; \\1\\2\\3\\4\\5
;; \\1\\2\\3\\4\\5

(disassemble (byte-compile 'make-backrefs))

    0       varref    total
    1       constant  0
    2       gtr
    3       goto-if-nil 1
    6       constant  format
    7       constant  "%s\\\\%s"
    8       constant  make-backrefs
    9       varref    total
    10      sub1
    11      call      1
    12      varref    total
    13      call      3
    14      return
    15:1    constant  ""
    16      return

(disassemble (byte-compile 'make-backrefs-while))

    0       constant  ""
    1       varbind   refs
    2       varref    total
    3:1     constant  0
    4       gtr
    5       goto-if-nil 2
    8       constant  format
    9       constant  "\\\\%s%s"
    10      varref    total
    11      varref    refs
    12      call      3
    13      varset    refs
    14      varref    total
    15      sub1
    16      dup
    17      varset    total
    18      goto      1
    21:2    varref    refs
    22      unbind    1
    23      return

(disassemble (byte-compile 'make-backrefs-tail))

    0       varref    refs
    1       goto-if-not-nil 1
    4       constant  ""
    5       varset    refs
    6:1     varref    total
    7       constant  0
    8       gtr
    9       goto-if-nil 2
    12      constant  make-backrefs-tail
    13      varref    total
    14      sub1
    15      constant  format
    16      constant  "\\\\%s%s"
    17      varref    total
    18      varref    refs
    19      call      3
    20      call      2
    21      return
    22:2    varref    refs
    23      return

-- 
underground experts united
http://user.it.uu.se/~embe8573




reply via email to

[Prev in Thread] Current Thread [Next in Thread]