File: utils.lisp

package info (click to toggle)
cl-awk 1-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 128 kB
  • ctags: 259
  • sloc: lisp: 1,012; makefile: 50; sh: 28
file content (24 lines) | stat: -rw-r--r-- 732 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;;;; -*- Mode: Lisp -*-

(in-package #:clawk)

;; Code taken from Peter Norvig's _Paradigms of AI Programming_ book

(defun side-effect-free-p (exp)
  (or (constantp exp) (atom exp) (starts-with exp 'function)
      (and (starts-with exp 'the)
	   (side-effect-free-p (third exp)))))

(defmacro once-only (variables &rest body)
  (assert (every #'symbolp variables))
  (let ((temps (loop repeat (length variables) collect (gensym))))
    `(if (every #'side-effect-free-p (list . ,variables))
       (progn . ,body)
       (list 'let
	     ,`(list ,@(mapcar #'(lambda (tmp var)
				   `(list ',tmp ,var))
			       temps variables))
	     (let ,(mapcar #'(lambda (var tmp) `(,var ',tmp))
			   variables temps)
	       . ,body)))))