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

2. Creating Your First Lisp Program > Defining Local Functions in Lisp

Defining Local Functions in Lisp

We define local functions using the flet command. The flet command has the following structure:

 (flet ((function_name (arguments)
          ...function body...))
   ...body...)

At the top of the flet, we declare a function (in the first two lines). This function will then be available to us in the body . A function declaration consists of a function name, the arguments to that function , and the function body , where we put the function’s code.

Here is an example:

 > (flet ((f (n)
             (+ n 10)))
     (f 5))
  15

In this example, we define a single function, f, which takes a single argument, n . The function f then adds 10 to this variable n , which has been passed in it. Then we call this function with the number 5 as the argument, causing the value 15 to be returned .


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

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

  

Start a Free 10-Day Trial


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