Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

14. Ramping Lisp Up a Notch with Functio... > Anatomy of a Program Written in the ...

Anatomy of a Program Written in the Functional Style

Now that we’ve discussed how functional programming is done, let’s write a simple program that follows this style. Since we want this program to be a typical example of most software, we should figure out what most software in the world actually does. So what do most programs in the world actually do? They keep track of widgets!

image with no caption

Here’s our entire example program, written in the functional style:

;the clean, functional part
 (defun add-widget (database widget)
   (cons widget database))

  ;the dirty, nonfunctional part
 (defparameter *database* nil)

 (defun main-loop ()
   (loop (princ "Please enter the name of a new widget:")
         (setf *database* (add-widget *database* (read)))
          (format t "The database contains the following: ˜a˜%" *database*)))

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint