1 ;;; tl-list.el --- utility functions about list
3 ;; Copyright (C) 1987 .. 1996 Free Software Foundation, Inc.
4 ;; Copyright (C) 1997 MORIOKA Tomohiko
6 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;; Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
9 ;; Version: $Id: tl-list.el,v 1.1.1.1 1998-01-14 06:27:57 steve Exp $
12 ;; This file is part of XEmacs.
14 ;; This program is free software; you can redistribute it and/or
15 ;; modify it under the terms of the GNU General Public License as
16 ;; published by the Free Software Foundation; either version 2, or (at
17 ;; your option) any later version.
19 ;; This program is distributed in the hope that it will be useful, but
20 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 ;; General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with This program; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27 ;; Boston, MA 02111-1307, USA.
31 (require 'file-detect)
33 (cond ((file-installed-p "cl-seq.elc")
37 ;; New cl is not exist (Don't use old cl.el)
39 (defun last (ls &optional n)
40 "Returns the last element in list LS.
41 With optional argument N, returns Nth-to-last link (default 1).
42 \[tl-list.el; tomo's Common Lisp emulating function]"
43 (nthcdr (- (length ls) (or n 1)) ls)
46 ;; imported from cl.el
47 (defun list* (arg &rest rest)
48 "Return a new list with specified args as elements, cons'd to last arg.
49 Thus, `(list* A B C D)' is equivalent to `(nconc (list A B C) D)', or to
50 `(cons A (cons B (cons C D)))'."
51 (cond ((not rest) arg)
52 ((not (cdr rest)) (cons arg (car rest)))
53 (t (let* ((n (length rest))
54 (copy (copy-sequence rest))
55 (last (nthcdr (- n 2) copy)))
56 (setcdr last (car (cdr last)))
59 (defconst :test ':test)
61 (defun MEMBER (elt list &rest keywords)
64 (let ((ret (memq ':test keywords)))
77 (let* ((cell (car list))
78 (ret (funcall test elt cell))
83 (setq list (cdr list))
86 (defun ASSOC (key alist &rest keywords)
89 (let ((ret (memq ':test keywords)))
102 (let* ((cell (car alist))
103 (ret (funcall test key (car cell)))
108 (setq alist (cdr alist))
112 (autoload 'compress-sorted-numbers "range")
113 (autoload 'expand-range "range")
114 (autoload 'member-of-range "range")
120 (defun nnth-prev (n ls)
121 "Modify list LS to remove elements after N th. [tl-list.el]"
123 (let ((cell (nthcdr (1- n) ls)))
129 (defun nth-prev (n ls)
130 "Return the first N elements. [tl-list.el]"
132 (while (and (> n 0) ls)
133 (setq dest (cons (car ls) dest))
140 (defun nexcept-nth (n ls)
141 "Modify list LS to remove N th element. [tl-list.el]"
145 (let ((cell (nthcdr (1- n) ls)))
147 (setcdr cell (cdr (cdr cell)))
151 (defun except-nth (n ls)
152 "Return elements of LS except N th. [tl-list.el]"
156 (while (and (> n 0) ls)
157 (setq dest (cons (car ls) dest))
163 (setq ls (cons (car dest) ls))
164 (setq dest (cdr dest))
168 (defun last-element (ls)
169 "Return last element. [tl-list.el]"
173 (defun cons-element (elt ls)
174 "Cons ELT to LS if ELT is not nil. [tl-list.el]"
179 (defun cons-if (elt ls)
180 "Cons ELT to LS if LS is not nil, otherwise return nil. [tl-list.el]"
185 (defun append-element (ls elt)
186 "Append ELT to last of LS if ELT is not nil. [tl-list.el]"
188 (append ls (list elt))
192 ;;; @ permutation and combination
195 (defun every-combination (prev &rest rest)
196 "Every arguments are OR list,
197 and return list of all possible sequence. [tl-list.el]"
206 (rest-mixed (apply 'every-combination rest))
209 (let ((rr rest-mixed))
211 (setq dest (cons (cons (car pr)(car rr)) dest))
220 (defun permute (&rest ls)
221 "Return permutation of arguments as list. [tl-list.el]"
222 (let ((len (length ls)))
230 (setq rest (cdr rest))
237 (apply (function permute)
240 (setq prev (nconc prev (list c)))
249 (defun index (start end &optional inc)
250 "Return list of numbers from START to END.
251 Element of the list increases by INC (default value is 1).
252 \[tl-list.el; ELIS compatible function]"
256 (let ((pred (if (>= inc 0)
262 (while (funcall pred i end)
263 (setq dest (cons i dest))
273 (defun map-union (func ls)
274 "Apply FUNC to each element of LS.
275 And return union of each result returned by FUNC. [tl-list.el]"
276 (let ((r ls) ret rc dest)
278 (setq ret (funcall func (car r)))
282 (setq dest (cons rc dest))
297 (defun assoc-value (item alist)
298 "Return value of <ITEM> from <ALIST>. [tl-list.el]"
299 (cdr (assoc item alist))
306 (defun poly-funcall (functions arg)
308 (setq arg (funcall (car functions) arg)
309 functions (cdr functions))
322 ;;; tl-list.el ends here