r3182: *** empty log message ***
[reversi.git] / utils.lisp
1 ;;;;***************************************************************************
2 ;;;;
3 ;;;; FILE IDENTIFICATION
4 ;;;; 
5 ;;;;  Name:           reversi-base.lisp
6 ;;;;  Purpose:        Basic functions for reversi
7 ;;;;  Programer:      Kevin M. Rosenberg
8 ;;;;  Date Started:   1 Nov 2001
9 ;;;;
10 ;;;; $Id: utils.lisp,v 1.2 2002/10/25 09:23:39 kevin Exp $
11 ;;;;
12 ;;;; This file is Copyright (c) 2001-2002 by Kevin M. Rosenberg 
13 ;;;;
14 ;;;; Reversi 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 :reversi)
20 (declaim (optimize (safety 1) (debug 3) (speed 3) (compilation-speed 0)))
21
22
23 ;; Anaphoric macros
24
25 (defmacro aif (test then &optional else)
26   `(let ((it ,test))
27      (if it ,then ,else)))
28
29 (defmacro awhen (test-form &body body)
30   `(aif ,test-form
31         (progn ,@body)))
32
33 (defmacro awhile (expr &body body)
34   `(do ((it ,expr ,expr))
35        ((not it))
36      ,@body))
37
38 (defun mappend (fn list)
39   "Append the results of calling fn on each element of list.
40   Like mapcon, but uses append instead of nconc."
41   (apply #'append (mapcar fn list)))
42
43 (defun random-elt (seq) 
44   "Pick a random element out of a sequence."
45   (elt seq (random (length seq))))
46
47 (defun concat-symbol (&rest args)
48   "Concatenate symbols or strings to form an interned symbol"
49   (intern (format nil "~{~a~}" args)))
50
51 (defun cross-product (fn xlist ylist)
52   "Return a list of all (fn x y) values."
53   (mappend #'(lambda (y)
54                (mapcar #'(lambda (x) (funcall fn x y))
55                        xlist))
56            ylist))
57
58
59 (defmacro until (test &body body)
60   `(do ()
61        (,test)
62      ,@body))
63
64 (defmacro while (test &body body)
65   `(do ()
66        ((not ,test))
67      ,@body))
68
69 #+excl
70 (defun list-to-delimited-string (list &optional (separator #\space))
71   (excl:list-to-delimited-string list separator))
72
73 #-excl
74 (defun list-to-delimited-string (list &optional (separator #\space))
75   (let ((output (when list (format nil "~A" (car list)))))
76     (dolist (obj (rest list))
77       (setq output (concatenate 'string output
78                                 (format nil "~A" separator)
79                                 (format nil "~A" obj))))
80     output))
81
82
83