1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Functions for lists for KMRCL package
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: lists.lisp,v 1.3 2003/05/06 01:43:14 kevin Exp $
12 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
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 ;;;; *************************************************************************
23 "Make into list if atom"
24 (if (listp obj) obj (list obj)))
26 (defun filter (fn lst)
27 "Filter a list by function, eliminate elements where fn returns nil"
30 (let ((val (funcall fn x)))
31 (if val (push val acc))))
34 (defun appendnew (l1 l2)
35 "Append two lists, filtering out elem from second list that are already in first list"
37 (unless (find elem l1)
38 (setq l1 (append l1 (list elem)))))
43 (defun remove-tree-if (pred tree)
44 "Strip from tree of atoms that satistify predicate"
46 (unless (funcall pred tree)
48 (let ((car-strip (remove-tree-if pred (car tree)))
49 (cdr-strip (remove-tree-if pred (cdr tree))))
51 ((and car-strip (atom (cadr tree)) (null cdr-strip))
53 ((and car-strip cdr-strip)
54 (cons car-strip cdr-strip))
60 (defun find-tree (sym tree)
61 "Finds an atom as a car in tree and returns cdr tree at that positions"
62 (if (or (null tree) (atom tree))
64 (if (eql sym (car tree))
66 (aif (find-tree sym (car tree))
68 (aif (find-tree sym (cdr tree))
73 (cond ((atom lis) lis)
75 (append (flatten (car lis)) (flatten (cdr lis))))
76 (t (append (list (car lis)) (flatten (cdr lis))))))
80 (defun remove-keyword (key arglist)
81 (loop for sublist = arglist then rest until (null sublist)
82 for (elt arg . rest) = sublist
83 unless (eq key elt) append (list elt arg)))
85 (defun remove-keywords (key-names args)
86 (loop for ( name val ) on args by #'cddr
87 unless (member (symbol-name name) key-names
88 :key #'symbol-name :test 'equal)
89 append (list name val)))
91 (defun mapappend (func seq)
92 (apply #'append (mapcar func seq)))
94 (defun mapcar-append-string-nontailrec (func v)
95 "Concatenate results of mapcar lambda calls"
97 (concatenate 'string (funcall func it)
98 (mapcar-append-string-nontailrec func (cdr v)))
102 (defun mapcar-append-string (func v &optional (accum ""))
103 "Concatenate results of mapcar lambda calls"
105 (mapcar-append-string
108 (concatenate 'string accum (funcall func it)))
111 (defun mapcar2-append-string-nontailrec (func la lb)
112 "Concatenate results of mapcar lambda call's over two lists"
116 (concatenate 'string (funcall func a b)
117 (mapcar2-append-string-nontailrec func (cdr la) (cdr lb)))
120 (defun mapcar2-append-string (func la lb &optional (accum ""))
121 "Concatenate results of mapcar lambda call's over two lists"
125 (mapcar2-append-string
129 (concatenate 'string accum (funcall func a b)))
132 (defun append-sublists (list)
133 "Takes a list of lists and appends all sublists"
134 (let ((results (car list)))
135 (dolist (elem (cdr list))
136 (setq results (append results elem)))