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