alternate data structure and function

29 views
Skip to first unread message

John de la Garza

unread,
Jul 25, 2017, 11:12:04 PM7/25/17
to land-o...@googlegroups.com
In the chapter five wizards_game.lisp program there are data
structures that represent objects at locations. The original
code uses a list of symbols to represent objects and an a-list
that provides an object to location mapping.

(defparameter *objects* '(whiskey bucket frog chain))

(defparameter *object-locations*
'((whiskey living-room)
(bucket living-room)
(chain garden)
(frog garden)))


Why not remove the redundant *objects* list and just use the keys
from the a-list?

The function objects-at would be reimplemented to not need the
object list to be passed in.

orig:
(defun objects-at (loc objs obj-loc)
(labels ((is-at (obj)
(eq loc (cadr (assoc obj obj-loc)))))
(remove-if-not #'is-at objs)))

to either:
(defun jj-objects-at (loc obj-locs)
(let ((objs (mapcar #'car obj-locs)))
(labels ((is-at (obj)
(eq loc (cadr (assoc obj obj-locs)))))
(remove-if-not #'is-at objs))))

or more inline less nesting:
(defun jj-objects-at2 (loc obj-locs)
(labels ((is-at (obj)
(eq loc (cadr (assoc obj obj-locs)))))
(remove-if-not #'is-at (mapcar #'car obj-locs))))

Edward Kenworthy

unread,
Jul 26, 2017, 1:40:42 AM7/26/17
to land-o...@googlegroups.com

Hi

 

Three reasons I can think of for why not:

 

  1. The intent is much less clear with your version.
  2. It doesn’t support multiple objects at a location.
  3. It’s less performant, because it has to unnecessarily perform (let ((objs (mapcar #'car obj-locs))) on every call.

 

Or to reverse that: *objects* would be an optimisation of your code improving performance, which supports multiple objects at a location and makes the intent clearer.

 

Edward

--
You received this message because you are subscribed to the Google Groups "Land of Lisp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to land-of-lisp...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Edward Kenworthy

unread,
Jul 26, 2017, 1:42:01 AM7/26/17
to land-o...@googlegroups.com

And P.S.

 

4. It doesn’t support objects that aren’t at a location.

 

Sent from Mail for Windows 10

John de la Garza

unread,
Jul 26, 2017, 11:01:46 PM7/26/17
to land-o...@googlegroups.com
On Wed, Jul 26, 2017 at 05:41:59AM +0000, Edward Kenworthy wrote:
> Three reasons I can think of for why not:
>
>
> 1. The intent is much less clear with your version.
> 2. It doesn’t support multiple objects at a location.
> 3. It’s less performant, because it has to unnecessarily perform (let ((objs (mapcar #'car obj-locs))) on every call.
> 4. It doesn’t support objects that aren’t at a location.
>
> Or to reverse that: *objects* would be an optimisation of your code improving performance, which supports multiple objects at a location and makes the intent clearer.

I knew I was missing something...

If I had actually used the new code and played the game, 2 and 4 would
have caused issues and I would have been pushed back to the original.

I was aware of 3, but decided that I didn't care. For 1, I was thinking,
at least at the time I did this, that the new structure was more clear,
but now I am onboard with the original structure.

Thanks,

John de la Garza
Reply all
Reply to author
Forward
0 new messages