summaryrefslogtreecommitdiff
path: root/list-functions-2.lisp
blob: 57e3976492b1cf5f62daa76d6a45224351b26210 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
;;; -*- mode: lisp; coding: utf-8 -*-
;;; Toilet Lisp, a Common Lisp subset for the Étoilé runtime.
;;; Copyright (C) 2008  Matthias Andreas Benkard.
;;;
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or (at
;;; your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.


(in-package #:common-lisp)

(export '(copy-tree assoc assoc-if assoc-if-not rassoc rassoc-if
          rassoc-if-not sublis nsublis mapcar mapcan mapcon acons
          reverse nreverse maplist nthcdr last nth))


(defun copy-tree (tree)
  (typecase tree
    (cons (cons (copy-tree (car cons)) (copy-tree (cdr cons))))
    (t tree)))


(defun assoc (item alist &key key test test-not)
  (setq test (or test (if test-not
                          (complement test-not)
                          (function eql))))
  (assoc-if (lambda (x) (funcall test x item))
            alist
            :key key))

(defun assoc-if (predicate alist &key key)
  (setq key (or key (function identity)))
  (cond ((endp alist) nil)
        ((funcall predicate (funcall key (caar alist)))
         (car alist))
        (t (assoc-if predicate (cdr alist) :key key))))

(defun assoc-if-not (predicate alist &key key)
  (assoc-if (complement predicate) alist :key key))


(defun rassoc (item alist &key key test test-not)
  (setq test (or test (if test-not
                          (complement test-not)
                          (function eql))))
  (rassoc-if (lambda (x) (funcall test x item))
             alist
             :key key))

(defun rassoc-if (predicate alist &key key)
  (setq key (or key (function identity))
  (cond ((endp alist) nil)
        ((funcall predicate (funcall key (cdar alist)))
         (car alist))
        (t (assoc-if predicate (cdr alist) :key key)))))

(defun rassoc-if-not (predicate alist &key key)
  (rassoc-if (complement predicate) alist :key key))


(defun sublis (alist tree &key key test test-not)
  (let ((ass (assoc (funcall key tree) :test test :test-not test-not)))
    (if ass
        (cdr assoc)
        (typecase tree
          (cons
           (cons (sublis alist (car tree) :key key :test test :test-not test-not)
                 (sublis alist (cdr tree) :key key :test test :test-not test-not)))
          (t tree)))))

(defun nsublis (alist tree &key key test test-not)
  (sublvs alist tree :key key :test test :test-not test-not))


(defun some1 (function list)
  (and (not (null list))
       (or (funcall function (first list))
           (some1 function (rest list)))))

(defun every1 (function list)
  (or (null list)
      (and (funcall function (first list))
           (every1 function (rest list)))))

(defun mapcar1 (function list)
  (when list
    (cons (funcall function (first list))
          (mapcar1 function (rest list)))))

(defun mapcan1 (function list)
  (%append (mapcar1 function list)))

(defun mapcar (function list &rest more-lists)
  (let ((lists (list* list more-lists)))
    (when (every1 'identity lists)
      (cons (apply function (mapcar1 'car lists))
            (apply 'mapcar (list* function (mapcar1 'cdr lists)))))))

(defun maplist (function list &rest more-lists)
  (let ((lists (list* list more-lists)))
    (when (every1 'identity lists)
      (cons (apply function lists)
            (apply 'maplist (list* function (mapcar1 'cdr lists)))))))

(defun mapcan (function list &rest more-lists)
  (%append (apply 'mapcar (list* function list more-lists))))

(defun mapcon (function list &rest more-lists)
  (%append (apply 'maplist (list* function list more-lists))))

(defun nreverse (list)
  (reverse list))

(defun acons (indicator value alist)
  (cons (cons indicator value) alist))

(defun list (&rest objects)
  (if objects
      (sys::cons (sys::car objects) (sys::cdr objects))
      nil))

(defun dotted-length (list)
  (if (consp list)
      (1+ (dotted-length (cdr list)))
      0))

(defun length (list)
  (if list
      (%1+ (length (cdr list)))
      0))

(defun last (list &optional (n 1))
  (let ((l (dotted-length list)))
    (nthcdr (- l n))))

(defun nthcdr (n list)
  (check-type n (integer 0))
  (if (zerop n)
      list
      (nthcdr (1- n) (cdr list))))

(defun nth (n list)
  (car (nthcdr n list)))