Tuesday, May 10, 2016

#7 - Features of Lisp

Helloooooooo!

This blog will cover:
-Three interesting features of Lisp.
-Plans for future blog posts.

Math:
Lisp was invented at a time when computers were mainly used to make big math problems easier and to do them much faster than people.  They still are used for this today, but at the time Lisp's ability to deal with large numbers in an efficient way was more significant.  So one interesting feature of Lisp is it's mathematical capabilities.

Here are some examples:


-First is exponents - we can calculate 10^10 and 10^10^10^10 is similar amounts of time and with equal precision.
-Next is exact rational number calculations with a couple examples.
-Finally is an example of a complex number.  The #c(10 5) represents 10 + 5i.


Format:
Another interesting feature of Lisp is something that I have avoided using in my past examples and blog posts.  It is a format function that is used for output.  It seems elementary, and I didn't understand why a programmer would prefer it over a simple print statement.  Further research uncovers its power:

the format function, which syntactically looks like so:
 (format destination control-string arguments)
generally the destination is t which prints to the Standard Output stream, if the destination is nil, then the format returns a string and returns it.
In the control-string we decide what we want to see, but there are also directives that we can give the function to control what is printed, which makes it more than just a print function.
These symbols are built-in to the lisp environment, similar to regular expression symbols in Java.
Different symbols are used to represent broad categories for example:
~A - the following argument is an ASCII character
~S - the following argument is an S-expression
~D - the following argument is a decimal
~% - is used to add a new line to the print statement.


Thanks for reading,

The next blog post will be up soon.  It will be a program that utilizes the features discussed in this post.

Peter Short, CS 270, Blog 7



Sunday, May 8, 2016

#6 - Functions and Binary Search

Welcome back to another adventure exploring the programming language Lisp.

This blog will cover:
-writing and using functions
-writing a binary search function

First let's look at the syntax for declaring a function:

We use: (defun functionName (parameters) (actions of the function) )

An example of a function that finds a the average of three values is below:


Now that we know how to use a function, we want to write a binary search function.
In order to do so, we need to know how to make an array in Lisp.

Syntax: (setf arrayName (make-array '(size)))

Then we use the aref function to refer to values in the array.  Which we need to do to set the value of each...
Setting value of first cell Syntax: (setf (aref arrayName cellNumber) value)

Example where we create a valArray and fill it with numbers in a random order:

Next we can write the binary search.  It will be a function that takes in a sorted array, and a value to search for as parameters.

To best examine the binary search, let's provide an example, then walkthrough the code in the example.

The function, binary-search, and the array.  I then loaded the array with even values from 2 to 16:
The array looks like this:
index : value
0 : 2
1 : 4
2 : 6
3 : 8
4 : 10
5 : 12
6 : 14
7 : 16


Then we when run the search we get:


This tells us that the value 12 that we looked for is at index 5 in the array, which is correct!

What is the function doing?
First, we use the let function to assign a low and high value, the low being 0 always, and high being the length of the array - 1.
Next we use a do loop to move the middle value in the array to narrow down the location of the value that we are looking for.
Finally, once we find the value, we can return the index of where the value is inside of the array.

Thanks for reading.

Peter Short, CS 270, Blog 6



Tuesday, March 8, 2016

#5 - Loops, loops, loops, etc.

This blog will attempt to cover:
- loops in Lisp
- loop for in Lisp
- do loops in Lisp
- how loops and ifs can work together in Lisp

The most basic loop in Lisp is just loop.  It loops the action until it sees a condition and return statement.

An example of a basic loop:



Notes on the example:
(terpri) is a command that forces a new line. Otherwise the values would print on one line.
The last line with (when) is the condition that must be met.  Then it returns a.
Without the return statement, the loop function will go on infinitely.
This example is taken from the site I am learning from, http://www.tutorialspoint.com/lisp/lisp_loop_construct.htm
The loop function is most closely related to the while in Java.

Next, let's look at the loop for function.

Basic syntax:
(loop for variable from value1 to value2
      do ( action )
)
Important to know that the loop goes from value1 to value2 inclusively, as the example below shows:



We'll look at another version of the loop for later in this post.

Next, let's look at the do loop.

Basic syntax:
(do ( ( variable1 startvalue1 updatedvalue1 )
         ( variable2 startvalue2 updatedvalue2 )
          ...
         ( (testcondition) (return value) )
         (s expressions)
       )
)

s expressions would be used to print the values.  We see this in the example below using a function called format, which allows the user to print text and variables.  This example was taken from the same site, here's a link: http://www.tutorialspoint.com/lisp/lisp_do.htm



Finally, let's go back to the loop for and play around with an if conditional within the loop.
The syntax is similar to the loop for above, but an if statement is added before the do statement.
The example below shows us multiples of 8 in the range from 1 to 32.



Thanks for reading, I hope this has been interesting.  Loops work similar to how a loop would work in Java, and like most things with Lisp, the major difference is in the syntax and how to order the code so that it generates the correct response.

Have a good one,

Peter Short, CS 270, Blog 5


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





Tuesday, January 26, 2016

Self-Taught Language Blog Entry #1: Introduction and Resources

As the second oldest high-level programming language (Fortran is one year older), LISP was first conceived in 1958 as a better notation of mathematical calculations for computer programs.  Lisp pioneered AI and many concepts in computer science including conditionals, high-order functions, and recursion.  It is a family of computer programming languages known for the distinctive use of parentheses.  Today the two most widely used dialects are Common Lisp and Scheme, I shall be focusing on Common Lisp.

In order to test and run programs in Common Lisp I will utilize the CMUCL implementation of Common Lisp, found here: http://www.cons.org/cmucl/index.html

Other online resources for this project include:

Considering two books:
Common Lisp. The Language. 2nd Edition -- By Guy Steele
Practical Common Lisp, 1st Edition -- By Peter Seibel

LISP was designed by John McCarthy while he taught at MIT.  The original intent of the language was to solve mathematical problems, but it quickly became an effective language for developing artificial intelligence and advanced computer science concepts.
Common Lisp is considered a dialect of Lisp, and it first appeared in 1984.  It continues to be applied to uses in math and artificial intelligence.

Peter Short, CS 270, Jan. 27, 2016, Blog Post #1