Lisp Books

Showing posts with label predicates. Show all posts
Showing posts with label predicates. Show all posts

Sunday, 25 April 2010

Using AND NOT and OR in Lisp

Today's Lisp tutorial looks at using AND NOT and OR in Lisp, but firstly let's have a look at the answer to the challenge set in our last Lisp tutorial Using LET in Lisp:

Write a Lisp function called GUESS, where you try and guess the number thrown by a dice, this will have several parts.This function will randomly choose from 6 possibilities. You call the function with your guess of 1, 2, 3, 4 5 or 6, the function randomly chooses 6 choices and tells you if your guess was right or wrong.

(defun guess (n)
  (let ((dice (+ 1 (random 6))))
    (cond ((equalp dice n )  (list  'you 'guessed 'right 'it 'was dice))
          (t (list 'you 'guessed 'wrong 'it 'was dice)))))

The NOT Predicate in Lisp
We've looked at predicates in Lisp in a previous Lisp tutorial post. Predicates return T or  NIL if the statement is true or false. In Lisp T is true and NIL is false. Using NOT turns T into NIL and NIL into T.

For example using the ZEROP predicate we can test if the number is zero:
(zerop 4)
NIL



(zerop 0)
T


Using NOT we can check to see if a number is not zero:
(not (zerop 4))
T

(not (zerop 0))
NIL

The AND Predicate in Lisp
The AND Predicate in Lisp takes two inputs, both of these must be true for it to evaluate and return T otherwise it returns NIL.
(and (> 6 4) (< 1 3))
T

Here only one of our predicates is true:
(and (numberp 6) (equalp 1 9))
NIL

The OR Predicate in Lisp
The OR Predicate in Lisp returns T if either one or both arguments are true:
(or (numberp 6) (equalp 1 9))
T

(or (> 7 6) (equalp 1 1))
T

Today's Lisp Challenge
Write a function called BETWEEN that takes 3 input arguments N, MIN and MAX. This function tests to see if N is between MIN and MAX.

Subscribe to Programming Lisp Tutorial Blog and check back to see the solution!

Tuesday, 23 March 2010

Predicates In Lisp

Today we're looking at predicates. Predicates are tests that we can use to see if something is true or false.

Before we do that let's look at the answers from yesterday:

(defun minus-one (x)
   (- x 1))

(defun ten-times-bigger (x)
   (* x 10))

(defun weeks-to-days (weeks)
   (* weeks 7))

It doesn't matter if you called weeks x or something entirely different, it is often useful to give our arguments names that are useful.

Predicates
As mentioned earlier predicates are tests that return either True or False. In lisp:

T = True
Nil = False

Here are some examples:

oddp checks to see if a number is odd:

(oddp 5)
T

(oddp 6)
NIL

evenp checks to see if a number is even:

(evenp 5)
NIL

(evenp 6)
T

Here are some other common predicates with examples:
(zerop 0)
T

(= 3 4)
NIL

(> 3 4)
NIL

(< 5 10)
T



Predicate Summary
  • (evenp n) true if n is even
  • (floatp n) true if n is a floating point number
  • (integerp n) true if n is an integer
  • (minusp n) true if n is less than zero
  • (numberp n) true if n is a number 
  • (oddp n) true if n is an odd number (0 is considered even) 
  • (plusp n) true if n is greater than 0 
  • (zerop n) true if n is 0
  • (> x y) true if x is greater than y
  • (< x y) true if x is less than y
  • (>= x y) true if x is greater than or equal to y
  • (<= x y) true if x is less than or equal to y
Using Predicates In Functions
In yesterday's post we wrote our own functions. We can include predicates in these functions. For example, this function adds two numbers together and checks to see if the result is even, again we could all it almost anything but as it's a predicate that check to see if the sum of two numbers is even we'll call it sum-even-p:

(defun sum-even-p (x y)
   (evenp (+ x y)))



Notice the order of the function

    (evenp (+ x y)))

the input element to evenp is the + function and x y


Today's Challenge
Today's challenge write a function called square-odd-p. This function takes a single number as input, it squares this number and tests to see if the square of the number is odd.

Check tomorrow's post for the answer and more Lisp tutorials.