r4846: Auto commit for Debian build
[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.4 2003/05/06 15:51:20 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
21 (eval-when (:compile-toplevel)
22   (declaim (optimize (safety 1) (space 0) (speed 3) (compilation-speed 0))))
23
24 (defmacro missing-argument ()
25   `(error "Missing an argument to a constructor"))
26
27 ;; Anaphoric macros
28
29 (defmacro aif (test then &optional else)
30   `(let ((it ,test))
31      (if it ,then ,else)))
32
33 (defmacro awhen (test-form &body body)
34   `(aif ,test-form
35         (progn ,@body)))
36
37 (defmacro awhile (expr &body body)
38   `(do ((it ,expr ,expr))
39        ((not it))
40      ,@body))
41
42 (defun mappend (fn list)
43   "Append the results of calling fn on each element of list.
44   Like mapcon, but uses append instead of nconc."
45   (apply #'append (mapcar fn list)))
46
47 (defun random-elt (seq) 
48   "Pick a random element out of a sequence."
49   (elt seq (random (length seq))))
50
51 (defun concat-symbol (&rest args)
52   "Concatenate symbols or strings to form an interned symbol"
53   (intern (format nil "~{~a~}" args)))
54
55 (defun cross-product (fn xlist ylist)
56   "Return a list of all (fn x y) values."
57   (mappend #'(lambda (y)
58                (mapcar #'(lambda (x) (funcall fn x y))
59                        xlist))
60            ylist))
61
62
63 (defmacro until (test &body body)
64   `(do ()
65        (,test)
66      ,@body))
67
68 (defmacro while (test &body body)
69   `(do ()
70        ((not ,test))
71      ,@body))
72
73 #+excl
74 (defun list-to-delimited-string (list &optional (separator #\space))
75   (excl:list-to-delimited-string list separator))
76
77 #-excl
78 (defun list-to-delimited-string (list &optional (separator #\space))
79   (let ((output (when list (format nil "~A" (car list)))))
80     (dolist (obj (rest list))
81       (setq output (concatenate 'string output
82                                 (format nil "~A" separator)
83                                 (format nil "~A" obj))))
84     output))
85
86
87