Wednesday, February 17, 2016

#4 - Conditionals

Welcome back to the LISP blog!

This blog will discuss:
- Conditionals in LISP

We're going to start off with a simple if statement.
The basic syntax structure looks like this:
(if ( [test-clause] ) ( [action if true] ) ( [action if false] ) )

We'll test to see if an integer is positive, if it is it'll return positive, if not, it will return NIL, this is LISP's version of false.




In LISP we can use a single quote ( ' ) as like a return statement for printing the result of our conditional... An example of this syntax is found in the next example where we look at some integers and determine if they're even or odd.


The code works by testing if any given value mod 2 is equal to 0, if this tests to be true, it must be even, so we return even, otherwise it must be false, so we return odd.

Next, we'd like to test one value with three conditions, whether it's positive, negative, or zero.  To do this, we'll create three variables, using defvar.


For the comparison we'll use a cond function, the syntax for which is described as:
( cond ( (test1) (action1) ) ( (test2) (action2) ) ... ( (testN) (actionN) ) )

So, we'll try using this feature of LISP to test our values.


Next, we'll combine the two tests, and change testval1 to 10.  We'll have the program to print whether the value is positive/ negative / or zero, and if the value is even or odd.

First changing the variable, easily done using the setq function:


Next, we'll use cond to complete the conditionals again:


Biggest challenge I found with this program was keeping track of the parentheses, so be careful with those.  I think that will be the biggest cause of errors going forward.

I hope this was enjoyable, leave a comment down below if you have any questions.

Peter Short, CS 270, February 17, 2016, Blog #4

Wednesday, February 10, 2016

#3 - Variables, Constants, and Math... oh my!

Welcome back to the LISP blog!

This blog will discuss:
- declaring and using variables in LISP
- declaring and using constants in LISP
- Arithmetic operators in LISP

We'll be doing the programming in this post using the Listener in LispWorks Personal Edition.

First, we'll declare a variable, x and assign it a value of 10 - seen below.  As well as declaring it, we have the Listener print the value of the variable using the write function, then again by simply calling the variable name in the command line:

As you can see, the syntax in LISP is unique.  Essentially when declaring a variable using the defvar function, it breaks down like so: ( defvar [name of variable] [value of variable])
Here are two more examples as we declare variables y and z.



Next let's do some math.  First, let me exemplify the four basic operators, addition, subtraction, multiplication, and division.  The syntax in LISP can be confusing when using arithmetic operators.
Here are some examples, notice that the operator comes first.  When the line is read, it continues to do the operation until every value is read.


Next, we will declare a variable, total, and assign it the value of x + y + z:
We then call the variable so that the value can be printed, showing that we have the correct total.


Next, we'll find the average of the three values by declaring another variable, avg, using the division function to divide the total by 3, then calling the variable we created, avg.


Everything checks out okay!

Now we're going to declare a constant using the defconstant function.  Let's define the constant gravity at 9.8 m/s^2 and then we'll do some physics calculations with it.


Let's look at the general formula for velocity with respect to time: v = gt, where v is velocity, g is gravity, and t is time.  As an example of LISPs arithmetic abilities, let's pretend our variables x, y, and z are times in seconds, and our constant gravity will act as gravity.  We can then find the velocity of any object at the times 10, 20, and 30 using variables x, y, and z respectively.



That's all for today, I hope you enjoyed.

If you have any questions, feel free to leave a comment below.

Peter Short, CS 270, February 10, 2016, Blog #3






Wednesday, February 3, 2016

#2 - Hello, World! or maybe Hello, again?

In my first blog post I had said I would use the CMUCL Implementation of Common Lisp, and provided a link.  As I read into the tutorial site I plan on using to learn the language, it recommends having a text editor for bigger and more complex programs.  As I looked into solving this issue without changing compilers, it became clear that a change would be necessary.  I have since found an IDE for Common Lisp that works on MAC OS X - LispWorks.

If you would like to try out Lisp using LispWorks, here is a link to the download site:

LispWorks Download - http://www.lispworks.com/downloads/index.html

It has become clear through some tests using LispWorks that it will be far more efficient to use than CMUCL, so a switch is for the best.

Welcome! Hopefully this blog will be mildly entertaining for all readers...

In LispWorks, the Listener acts as a command line executer of Lisp code.  The string, "Hello, World!" can be displayed by simply writing out Hello, World, and surrounding it with quotes (" ").  A screenshot below shows the execution of a very simple Hello, world! line of Lisp code:



Another way to write code in LispWorks is to use the Editor.

Below is a variation of the Hello World program using the write function and showing the use of parentheses in Lisp, while also showing semi-colons ( ; ) as the character used for comments.  The code below is written in the LispWorks editor:


After pressing the compile button, we are taken to the output tab, and shown the output:


The compiler provides other information with the output, which could provide more information about bugs in our code.

---

Overall, my experiences working with Lisp have been very reasonable.  The Hello World program is very easy to run, and I've begun exploring some of the math functions of Lisp, which are neat and I look forward to exploring further.

Peter Short, CS 270, Feb. 3, 2016, Blog #2