....I am Lisp beginner and I am doing a simple execise on lisp. I was asked
to write a lisp program , by using either recursion or do, to return
true if the difference between each successive pair of the them is 1.
ie:
(difference '(2 1))T
(difference'(3 4 5 6 5 4))T
(differnce '(3 4 5 3))NIL
(defun difference (param)
(if (> (length param) 1)
(if (<= (abs (- (first param) (first (rest param)))) 1 )
(difference (rest param))
nil
)
T
)
)
I am Lisp beginner and I am doing a simple execise on lisp. I was asked to write a lisp program , by using either recursion or do, to return
true if the difference between each successive pair of the them is 1.
ie:
....(difference '(2 1))T
(difference'(3 4 5 6 5 4))T
(differnce '(3 4 5 3))NIL
(defun difference (param)
(if (> (length param) 1)
(if (<= (abs (- (first param) (first (rest param)))) 1 )
(difference (rest param))
nil
)
T
)
)
Gauche Scheme
(define (difference xs)
(do ((ok #t))
((or (null? xs) (not ok)) ok)
(let1 x (pop! xs)
(if (pair? xs)
(set! ok (= 1 (abs (- x (car xs)))))))))
B. Pym wrote:
I am Lisp beginner and I am doing a simple execise on lisp. I was asked to write a lisp program , by using either recursion or do, to return true if the difference between each successive pair of the them is 1.
ie:
....(difference '(2 1))T
(difference'(3 4 5 6 5 4))T
(differnce '(3 4 5 3))NIL
(defun difference (param)
(if (> (length param) 1)
(if (<= (abs (- (first param) (first (rest param)))) 1 )
(difference (rest param))
nil
)
T
)
)
Gauche Scheme
(define (difference xs)
(do ((ok #t))
((or (null? xs) (not ok)) ok)
(let1 x (pop! xs)
(if (pair? xs)
(set! ok (= 1 (abs (- x (car xs)))))))))
Shorter.
(define (difference xs)
(do ((ok #t
(let1 x (pop! xs)
(if (null? xs) #t (= 1 (abs (- x (car xs))))))))
((or (null? xs) (not ok)) ok)))
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 59 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 19:37:38 |
| Calls: | 810 |
| Calls today: | 1 |
| Files: | 1,287 |
| D/L today: |
10 files (21,017K bytes) |
| Messages: | 194,291 |