....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: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 06:31:13 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
921 files (14,318M bytes) |
| Messages: | 264,702 |