next up previous
Next: 4.6 リスト構造のプリント Up: 4 リストの編集プログラム Previous: 4.4 アトムコマンドの処理

4.5 リスト形コマンドの処理

リスト型のうちリストの先頭要素がコマンドの種類を指定します. 先頭要素が数の場合には,ledit-list-number-cmdにより処理されます.
(defun ledit-list-number-cmd (cmd arg)
  (cond
   ((null arg) (nth-delete (1- cmd) *sexp*))
   ((< cmd 0) (nth-insert-list (- -1 cmd) arg *sexp*))
   (t (setq cmd (1- cmd))
      (dolist (x arg)
         (nth-insert cmd x *sexp*)
         (setq cmd (1+ cmd)))
      (nth-delete cmd *sexp*))))
;;;
(defun ledit-list-symbol-cmd (cmd arg)
  (case
   cmd
   (n (nconc *sexp* arg))
   (r (nsubst (cadr arg) (car arg) *sexp*))
   (f (setq *sexp* (or (member+ (car arg) *sexp*) *sexp*)))
   (bi (if (null (cdr arg))
           (both-in (car arg) (car arg) *sexp*)
         (both-in (car arg) (cadr arg) *sexp*)))
   (bo (both-out (car arg) *sexp*))
   (e (print (eval (cons 'progn arg))))
   (cdr (setq *sexp* (nthcdr (car arg) *sexp*)))
   (t (print '?))))
;;;
;;;
(defun both-in (from to sexp)
  (nth-enclose (1- from) (1- to) sexp))
(defun both-out (loc sexp) (nth-extend (1- loc) sexp))
;;;
(defun ledit-help nil
  (mapc #'(lambda (x) (princ x) (terpri))
        '(
  "  usage: (ledit object)"
  "h or ?-- help"
  "ok    -- endq-- quit (not saved)"
  "p     -- level printpp-- pretty print"
  "mark  -- stack cur_pointer^ or top-- top level"
  "<     -- pop cur_pointer"
  "num   -- into the num-th element"
  "- num -- into the num-th element from the last"
  "cdr   -- into cdr part"
  "(bi n1 n2)-- both in from n1 to n2 elements"
  "(bo n)    -- both out from n-th element"
  "(f s-exp) -- find s-exp"
  "(r s-exp s'-exp)-- substitute all s-exp to s'-exp"
  "(cdr n)   -- n-th cdr"
  "(n s-exp) -- append s-exp to the last"
  "(num)     -- delete num-th element"
  "(num { s-exp })-- substitute num-th element to s-exp"
  "(-num { s-exp })-- insert s-exp into num-th element"
  "(e { form1 .. formn })-- eval from form1 to formn"
  )) t)
リスト構造の中からシンボルだけでなくリストデータをも探索する関数 member+は以下のように定義できる.
(defun member+ (item l)
  (cond
   ((atom l) nil)
   ((equal item (car l)) l)
   ((member+ item (car l)))
   ((member+ item (cdr l)))))


generated through LaTeX2HTML. M.Inaba 平成18年5月7日