Learning Lisp

Yesterday, I happen to read a comic based Lisp tutorial at www.lisperati.com. It was fun to go through the tutorial which very easily introduces to Lisp and creates a Text-based adventure game in Lisp.

After playing the game, I build up my first Emacs Lisp function to restart the game.


Restart function in Emacs Lisp

(defun restart ()
         (setq object-locations
           (remove-if (lambda (x)
             (eq 'body (second x)))

On line two I have used progn function, that takes a list of forms to execute and returns the result of last form execution. In this function, I have two forms, first one to clear from object-locations the user inventory i.e. items picked in the play. The second form starts the game with look function

To reset user inventory, I have used remove-if function to clear from object-locations list all items that belongs to user body and then set the result list back to object-locations. Even though this is not pure Functional programming, it is step towards it as instead of modifying the existing list, a list is created by filtering and assigned as new object-locations list.

Happy Lisping.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s