I'm going through Casting SPELs in Lisp and this is the proposed solution to handling picking up objects:
(define *location* 'living-room)
(define *object-locations*
'((whiskey-bottle living-room)
(bucket living-room)
(chain garden)
(frog garden)))
(define (pickup-object object)
(cond [(is-at? object *location* *object-locations*)
(push! (list object 'body) *object-locations*)
(string-append "You're now carrying the " (symbol->string object) ".")]
[else "There's no such object in here."]))
Am I the only one who finds this inefficient? As far as I understand the push!
function cons
es a new pair
to *object-locations*
every time the player picks up an object. While this may not be a major problem in a small game like this, if one would to add the option of putting down items from the inventory, the *object-locations*
list could grow infinitely... Shouldn't pickup-object
replace the cdr
of (whiskey-bottle living-room)
, for example, instead of adding another copy of the pair
?
I'm new to Lisp and may be mistaken... Could somebody please explain whether my assumptions are right, and if so, what would be the best way to handle picking up of objects in a Lisp text adventure?
There are a few problems with the code:
But
In Common Lisp that's easy to change:
See the book Common Lisp: A Gentle Introduction to Symbolic Computation by David S. Touretzky for a really basic introduction to Lisp.