Pressing Positions in Injection SPEL

I go through SPEL casting in Lisp , and this is the proposed solution for handling collecting 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 no such object in here."]))

Am I the only one who considers this ineffective? As far as I understand, the function push! consadds a new pairto *object-locations*every time a player takes an object. Although this may not be a serious problem in such a small game, as if you could add the option to put items from inventory, the list *object-locations*could grow indefinitely ... Should you not pickup-objectreplace cdrof (whiskey-bottle living-room), for example, instead of adding another copy pair?

Lisp ... - , , , Lisp ?

+3
1

:

  • * object-locations * . . . .
  • , .
  • STRING-APPEND .

  • .
  • .
  • .
  • .

Common Lisp, :

(defvar *object-locations*
  (copy-tree
   '((whiskey-bottle living-room)
     (bucket living-room)
     (chain garden)
     (frog garden))))

(defun get-location (object)
  (second (assoc object *object-locations*)))

(defun set-location (object location)
  (setf (second (assoc object *object-locations*))
        location))

CL-USER > (get-location 'frog)
GARDEN

CL-USER > (set-location 'frog 'living-room)
LIVING-ROOM

CL-USER > (get-location 'frog)
LIVING-ROOM

CL-USER > *object-locations*
((WHISKEY-BOTTLE LIVING-ROOM)
 (BUCKET LIVING-ROOM)
 (CHAIN GARDEN)
 (FROG LIVING-ROOM))

. Lisp: . Lisp.

+1

Source: https://habr.com/ru/post/1790161/


All Articles