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

3 comments:

  1. Hi Peter. I was wondering for some of your print statements, such as when determining if a number is positive, negative, or zero, why does the print statement print twice? For example in CL-USER 24, testval1 is positive and the output is "positive""positive". I was curious how you got this and why. Thanks!

    ReplyDelete
    Replies
    1. Good question. In Lisp, the last value compiled is always printed. The best way to exemplify this case is looking back at the Hello World statement. When I only type "Hello World", only Hello World is read by the compiler and it is returned. If I write, "(print Hello World)", the function print is called, it prints the string, but then "Hello World" is the last read thing by the compiler, so it returns that as well. I think a better example is when we assign a number to the variable x, say 25. So when we do (print x) it prints the value 25 twice, the first time it's doing the print command, the second time it's returning the value of the variable x. If you assign the value 25 to x, and simply type "x" in the command line it would return "25" just once. Good question, I appreciate it.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete