r4981: Auto commit for Debian build
[kmrcl.git] / lists.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          lists.lisp
6 ;;;; Purpose:       Functions for lists for KMRCL package
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: lists.lisp,v 1.4 2003/05/11 21:51:43 kevin Exp $
11 ;;;;
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; KMRCL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package :kmrcl)
20
21
22 (defun mklist (obj)
23   "Make into list if atom"
24   (if (listp obj) obj (list obj)))
25
26 (defun filter (fn lst)
27   "Filter a list by function, eliminate elements where fn returns nil"
28   (let ((acc nil))
29     (dolist (x lst (nreverse acc))
30       (let ((val (funcall fn x)))
31         (if val (push val acc))))))
32
33 (defun appendnew (l1 l2)
34   "Append two lists, filtering out elem from second list that are already in first list"
35   (dolist (elem l2 l1)
36     (unless (find elem l1)
37       (setq l1 (append l1 (list elem))))))
38
39 (defun remove-tree-if (pred tree)
40   "Strip from tree of atoms that satistify predicate"
41   (if (atom tree)
42       (unless (funcall pred tree)
43         tree)
44     (let ((car-strip (remove-tree-if pred (car tree)))
45           (cdr-strip (remove-tree-if pred (cdr tree))))
46       (cond
47        ((and car-strip (atom (cadr tree)) (null cdr-strip))
48         (list car-strip))
49        ((and car-strip cdr-strip)
50         (cons car-strip cdr-strip))
51        (car-strip
52         car-strip)
53        (cdr-strip
54         cdr-strip)))))
55
56 (defun find-tree (sym tree)
57   "Finds an atom as a car in tree and returns cdr tree at that positions"
58   (if (or (null tree) (atom tree))
59       nil
60     (if (eql sym (car tree))
61         (cdr tree)
62       (aif (find-tree sym (car tree))
63           it
64         (aif (find-tree sym (cdr tree))
65             it
66             nil)))))
67
68 (defun flatten (lis)
69   (cond ((atom lis) lis)
70         ((listp (car lis))
71          (append (flatten (car lis)) (flatten (cdr lis))))
72         (t (append (list (car lis)) (flatten (cdr lis))))))
73
74 ;;; Keyword functions
75
76 (defun remove-keyword (key arglist)
77   (loop for sublist = arglist then rest until (null sublist)
78         for (elt arg . rest) = sublist
79         unless (eq key elt) append (list elt arg)))
80
81 (defun remove-keywords (key-names args)
82   (loop for ( name val ) on args by #'cddr
83         unless (member (symbol-name name) key-names 
84                        :key #'symbol-name :test 'equal)
85         append (list name val)))
86
87 (defun mapappend (func seq)
88   (apply #'append (mapcar func seq)))
89
90 (defun mapcar-append-string-nontailrec (func v)
91   "Concatenate results of mapcar lambda calls"  
92   (aif (car v)
93        (concatenate 'string (funcall func it)
94                     (mapcar-append-string-nontailrec func (cdr v)))
95        ""))
96
97
98 (defun mapcar-append-string (func v &optional (accum ""))
99   "Concatenate results of mapcar lambda calls"  
100   (aif (car v)
101        (mapcar-append-string 
102         func 
103         (cdr v) 
104         (concatenate 'string accum (funcall func it)))
105        accum))
106
107 (defun mapcar2-append-string-nontailrec (func la lb)
108   "Concatenate results of mapcar lambda call's over two lists"  
109   (let ((a (car la))
110         (b (car lb)))
111     (if (and a b)
112       (concatenate 'string (funcall func a b)
113                    (mapcar2-append-string-nontailrec func (cdr la) (cdr lb)))
114       "")))
115   
116 (defun mapcar2-append-string (func la lb &optional (accum ""))
117   "Concatenate results of mapcar lambda call's over two lists"  
118   (let ((a (car la))
119         (b (car lb)))
120     (if (and a b)
121         (mapcar2-append-string func (cdr la)  (cdr lb)
122                                (concatenate 'string accum (funcall func a b)))
123       accum)))
124
125 (defun append-sublists (list)
126   "Takes a list of lists and appends all sublists"
127   (let ((results (car list)))
128     (dolist (elem (cdr list) results)
129       (setq results (append results elem)))))
130