Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 23 |
Nodes: | 6 (0 / 6) |
Uptime: | 52:39:50 |
Calls: | 583 |
Files: | 1,139 |
D/L today: |
179 files (27,921K bytes) |
Messages: | 111,617 |
....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)))