1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: functions.lisp
6 ;;;; Purpose: Function routines for KMRCL package
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
12 ;;;; KMRCL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
20 "Memoize results of call to fn, returns a closure with hash-table"
21 (let ((cache (make-hash-table :test #'equal)))
22 #'(lambda (&rest args)
23 (multiple-value-bind (val foundp) (gethash args cache)
26 (setf (gethash args cache) (apply fn args)))))))
28 (defun memoize (fn-name)
29 (setf (fdefinition fn-name) (memo-proc (fdefinition fn-name))))
31 (defmacro defun-memo (fn args &body body)
32 "Define a memoized function"
33 `(memoize (defun ,fn ,args . ,body)))
35 (defmacro _f (op place &rest args)
36 (multiple-value-bind (vars forms var set access)
37 (get-setf-expansion place)
38 `(let* (,@(mapcar #'list vars forms)
39 (,(car var) (,op ,access ,@args)))
42 (defun compose (&rest fns)
44 (let ((fn1 (car (last fns)))
46 #'(lambda (&rest args)
49 :initial-value (apply fn1 args))))