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