Sujet : diff1p? -------- A simple lisp problem
De : HenHanna (at) *nospam* devnull.tb (HenHanna)
Groupes : comp.lang.lispDate : 16. Jun 2024, 02:20:31
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4lb4g$3mt2j$2@dont-email.me>
References : 1 2
User-Agent : Mozilla Thunderbird
On 6/15/2024 1:08 PM, HenHanna wrote:
On 6/14/2024 8:45 PM, B. Pym wrote:
Frank Schwieterman 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. (or LESS)
>
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
)
)
looks good. except for CADR (second) and ) ) )
(define (diff1p? x)
(or (null? x)
(null? (cdr x))
(and (= 1 (abs (- (car x) (cadr x)) ))
(diff1p? (cdr x)) )))
>
Gauche Scheme
>
(define (diff1? xs)
(every
(lambda (a b) (= 1 (abs (- a b))))
xs
(cdr xs)))
>
(diff1? '(2 3 4 3 4 5 6 7 8 9 8)) ===> #t
(diff1? '(2 3 4 3 4 5 6 7 8 9 8 0)) ===> #f
in Scheme (Gauche), is there a way to write it more like this ?
def diff1(x):
return all( abs(x[i] - x[i+1]) == 1 for i in range(len(x)-1) )