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 ()
        (progn
         (setq object-locations
           (remove-if (lambda (x)
             (eq 'body (second x)))
              object-locations))
         (look)))

Explanations:
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.

Advertisements