Page 71 - DCAP310_INTRODUCTION_TO_ARTIFICIAL_INTELLIGENCE_AND_EXPERT_SYSTEMS
P. 71
Unit 4: LISP
4.4.1 Defining Functions Notes
If we have used to using one of the more common enterprise languages like Java or C#.NET, the
following probably looks pretty familiar (example code will be written in java):
public class MyMath
{
public static int AddTogether(int oneNumber,int otherNumber)
{
return oneNumber + otherNumber;
}
}
This is a simple function for adding two numbers together. Now, here’s the same function, but
written in LISP:
(defun addTogether (oneNumber otherNumber)
(+ oneNumber otherNumber))
that’s a little smaller, isn’t it? Let’s examine some of the differences. First, the java example has
this “public static” at the beginning. In an imperative language, these are important specifiers.
“public” says that any code can use that function, both code within the MyMath class structure
and code elsewhere. Other modifiers like “protected” and “private” would restrict access to that
function to a subset of code (child classes, and only the defining class, respectively). “static”
means that this is a “class-level” function, or in other words, you don’t need to have an instance
of the MyMath class already constructed to use this function. We can call it without instantiating
any objects. If “static” were omitted, we would have to first instantiate a “MyMath” object, and
then make the function call.
So how is this information conveyed in LISP? Simple: it doesn’t need it. Access modifiers like
“public” have to do with whether code outside the class can use the function or not. Functional
programming doesn’t use classes, the AddTogether function is just a piece of code that adds two
numbers together and returns the result, and once it has been loaded into the runtime it can be
called from any other piece of code. The same goes for “static”: there aren’t any “objects” or
“classes” in functional programming, so there’s no need to specify whether the function belongs
to the class or to instances of the class; it just exists.
4.4.2 Defining Procedures
So far we’ve just used the Listener as a calculator, to evaluate expressions. In this section, we’re
going to make a huge leap forward and show you how to define your own procedures. Once
you’ve defined a procedure, it has the same status as the built-in procedures, like list and +. Let’s
define a procedure to return the average of two numbers.
In words, the procedure for finding the average of two numbers is:
Add the first number to the second number.
Divide the sum by 2.
Defining a Procedure – Defun
To define a procedure we use the special operator defun. This is an abbreviation for define
function, but LISP procedures are not strictly functions in the mathematical sense. We can write
the average procedure as follows:
(defun average (1st-number 2nd-number)
(/(+ 1st-number 2nd-number) 2))
The first argument to defun gives the name of the procedure, which we’ve chosen as average.
LOVELY PROFESSIONAL UNIVERSITY 65