HenHanna
2024-06-15 20:47:01 UTC
in Python, is this something you'd write using all() ?
https://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work
def diff1(x):
if len(x) <= 1: return True
for i in range(len(x) - 1):
if abs(x[i] - x[i+1]) != 1: return False
return True
def pd(x): print (x, diff1(x))
pd( [0,3]) # ===> #f
pd( [2,3,4,3]) # ===> #t
pd( [1,2,3,4,3,2,3]) # ===> #t
pd( [1,2,3,4,5,6,7,8,9,0]) # ===> #f
(define (diff1p? x)
(or (null? x)
(null? (cdr x))
(and (= 1 (abs (- (car x) (cadr x)) ))
(diff1p? (cdr x)) )))
find
find-tail
any
every
count
every pred clist1 clist2 . . . [Function]
[R7RS list] Applies pred across each element of clists, and returns
#f as soon as pred returns #f. If all application of pred
return a non-false value, [every] returns the last result of the
applications.
it sounds like it expands to a big (OR ........ )
______________________________ Not
(define (every? predicate lst) (and (map predicate lst)))
https://stackoverflow.com/questions/19389490/how-do-pythons-any-and-all-functions-work
def diff1(x):
if len(x) <= 1: return True
for i in range(len(x) - 1):
if abs(x[i] - x[i+1]) != 1: return False
return True
def pd(x): print (x, diff1(x))
pd( [0,3]) # ===> #f
pd( [2,3,4,3]) # ===> #t
pd( [1,2,3,4,3,2,3]) # ===> #t
pd( [1,2,3,4,5,6,7,8,9,0]) # ===> #f
(define (diff1p? x)
(or (null? x)
(null? (cdr x))
(and (= 1 (abs (- (car x) (cadr x)) ))
(diff1p? (cdr x)) )))
(diff1p? '(0 3)) ===> #f
(diff1p? '(2 3 4 3)) ===> #t
(diff1p? '(1 2 3 4 5 6 7 8 9 8)) ===> #t
(diff1p? '(1 2 3 4 5 6 7 8 9 8 0)) ===> #f
__________________________(diff1p? '(2 3 4 3)) ===> #t
(diff1p? '(1 2 3 4 5 6 7 8 9 8)) ===> #t
(diff1p? '(1 2 3 4 5 6 7 8 9 8 0)) ===> #f
find
find-tail
any
every
count
every pred clist1 clist2 . . . [Function]
[R7RS list] Applies pred across each element of clists, and returns
#f as soon as pred returns #f. If all application of pred
return a non-false value, [every] returns the last result of the
applications.
it sounds like it expands to a big (OR ........ )
______________________________ Not
(define (every? predicate lst) (and (map predicate lst)))