Page 68 - DCAP310_INTRODUCTION_TO_ARTIFICIAL_INTELLIGENCE_AND_EXPERT_SYSTEMS
P. 68
Introduction to Artificial Intelligence & Expert Systems
Notes Simple Math
Step one when picking up a language is usually to work with the simple things that are easily
defined. Namely, mathematics. It’s an easy way to break into an unfamiliar workspace since
there’s usually a lot of commonality across languages and it can be a more gentle introduction
than jumping right into a catalogue of basic functions. This is true when moving to LISP, but
perhaps less so than other languages because most languages stick to the elementary school
standard “5 + 4” for doing simple arithmetic operations. LISP, true to form (List Processing
language), treats arithmetic operators just like any other function call. Standard syntax is
something like this (the carrot is actually the prompt in the interpreter and is not part of the
language):
>(function arg1 arg2)
result
so addition is done like this:
>(+ 5 4)
9
This can feel strange since we’re used to a different order even in verbalizing the expression
(Five Plus Four), but think about the benefit of prefix notation when you start to add extra
arguments. Most languages chain together addition like this:
5 + 4 + 3 + 2 + 1
Notice any redundancy? We’ve used the “+” operator 4 times in one “sum” operation. Using
lisp, this same expression becomes:
>(+ 5 4 3 2 1)
15
More concise, and actually more natural when you think about the way you do your sums on
paper when you have no calculator:
10
14
12
+ 8
44
why express over and over that you are STILL doing addition? why not just use the operator
once? Subtraction is done the same way, but is a little more awkward since it’s not commutative
the way addition is, and since we usually think of a dash at the front of an expression when we
are signifying a “not” operation on the result.
>(- 5 4)
1
and multiplication and division are in the same vein:
>(* 10 5 2)
100
>(/30 6)
5
Pretty simple, but enough to get your feet wet and to start practicing with a new syntax. Next
time we’ll go just a little deeper and look at list manipulation.
4.3.2 List Manipulation—Step 2
Each item in the list has two things: a value (the item ON the stump) and a link to the rest of the
list (the string leading from the branch to the next stump). Therefore, you’re “list” of 10 items on
62 LOVELY PROFESSIONAL UNIVERSITY