From: Joe Ng
Subject: help for a lisp beginer.
Date: 
Message-ID: <348BCBD9.339F6D54@isu.edu>
Hi,

I am Lisp Beginner, and doing a xlisp problem as an execise. However, I
have a hard time to implement it in Lisp. I was asked to write a
function to input a list of numbers, and return T if the difference
between each successive pari of them is 1, otherwise return NIL.

Example:
>(diff '(5 6))
T
>(diff '(3 6))
NIL
>(diff '(3 4 2 1 0))
T

If you guys know how to do it just let me know.

From: Rainer Joswig
Subject: Re: help for a lisp beginer.
Date: 
Message-ID: <joswig-ya023180000912970312100001@news.lavielle.com>
In article <·················@isu.edu>, ·····@isu.edu wrote:

> Hi,
> 
> I am Lisp Beginner, and doing a xlisp problem as an execise. However, I
> have a hard time to implement it in Lisp. I was asked to write a
> function to input a list of numbers, and return T if the difference
> between each successive pari of them is 1, otherwise return NIL.
> 
> Example:
> >(diff '(5 6))
> T
> >(diff '(3 6))
> NIL
> >(diff '(3 4 2 1 0))
> T
> 
> If you guys know how to do it just let me know.


a) Write a "specification".

Define a function named "diff" with a list of numbers as a parameter.
 - if the list is empty return false
 - else if the list has only one element return false
 - else if the list has two elements return whether the difference 
   between the two elements is 1
 - else if the difference between the first two elements is 1 check
   the rest of the list and return the result
 - otherwise return false

b) Now write parentheses around it:

(defun diff (list)
  (let ((length-of-list (length list)))
    (flet ((difference-is-one-p (a b)
             (= (abs (- a b)) 1)))
      (cond ((= length-of-list 0) nil)
            ((= length-of-list 1) nil)
            ((= length-of-list 2)
             (difference-is-one-p (first list) (second list)))
            ((difference-is-one-p (first list) (second list))
             (diff (rest list)))
            (t nil)))))

c) Run it:

(assert (not (diff '())) ())
(assert (not (diff '(1))) ())
(assert (not (diff '(2 0))) ())
(assert (diff '(2 1)) ())
(assert (diff '(2 3)) ())
(assert (not (diff '(2 4))) ())
(assert (not (diff '(2 4 4))) ())
(assert (not (diff '(2 4 6 7))) ())
(assert (diff '(3 4 3 4)) ())
(assert (not (diff '(3 4 3 4 7))) ())
(assert (diff '(-1 -2 -3)) ())
(assert (not (diff '(0 0 0 0 0 1 1 1 1))) ())

-- 
http://www.lavielle.com/~joswig/
From: Barry Margolin
Subject: Re: help for a lisp beginer.
Date: 
Message-ID: <66k0dv$7tl@tools.bbnplanet.com>
In article <·················@isu.edu>, Joe Ng  <·····@isu.edu> wrote:
>I am Lisp Beginner, and doing a xlisp problem as an execise. However, I
>have a hard time to implement it in Lisp. I was asked to write a
>function to input a list of numbers, and return T if the difference
>between each successive pari of them is 1, otherwise return NIL.

Didn't someone post this exact same problem a few weeks ago?

If you want help, at least show us what you've tried so we can try to help
you understand where you've gone wrong.  Don't just ask for the solution,
you won't learn anything that way.

-- 
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.