Schematic: how to check if all list items are identical

I would like to create a Scheme function that returns true if it is given a list consisting entirely of identical elements. Such a list would be "(1 1 1 1). That would give a lie with something like" (1 2 1 1).

This is what I have so far:

(define (list-equal? lst) (define tmp (car lst)) (for-each (lambda (x) (equal? x tmp)) lst) ) 

This is clearly not true, and I am new to this. I think I can not express the step at which I should return #t or #f .

Thanks in advance!

EDIT: I stumbled a bit and found a solution that works very well, and with a minimal amount of code:

 (define (list-equal? lst) (andmap (lambda (x) (equal? x (car lst))) lst)) 

Thanks again for helping everyone.

+4
source share
9 answers

The minimum amount of code, if you do not care that it only works for numbers:

 (define (list-equel? lst) (apply = lst)) 

Examples:

 > (list-equel? '(1 1 2 1)) #f > (list-equel? '(1 1 1 1)) #t > (list-equel? '(1)) #t 
+3
source

The andmap solution andmap nice, but if andmap not available, you can use this. It uses basic operations (and, or, null checking, equality checking) and processes empty lists and lists of elements. Similar to the Sean implementation, but no helper definition required.

 (define (list-equal? args) (or (or (null? args) (null? (cdr args))) (and (eq? (car args) (cadr args)) (list-equal? (cdr args))))) 
+1
source
 (define (list-equal? lst) (if (= (cdr lst) null) true (and (equal? (car lst) (cadr lst)) (list-equal? (cdr lst))))) 
0
source

Try something like this:

 (define (list-equal? lst) (define (helper el lst) (or (null? lst) (and (eq? el (car lst)) (helper (car lst) (cdr lst))))) (or (null? lst) (helper (car lst) (cdr lst)))) 

This may not be the purest implementation, but I think that it will correctly handle cases of empty lists and singleton lists.

0
source

R6RS has a for-all function that takes a predicate and a list and returns #t if the predicate returns true for all elements in the list and #f otherwise, which is exactly what you need here.

So, if you use R6RS (or any other dialect of the circuit that has a for-all function), you can simply replace for-each with for-all in your code, and it will work.

0
source

Something like this should work:

 (define (list-equal? lst) (cond ((< (length lst) 2) #t) (#t (and (equal? (car lst) (cadr lst)) (list-equal? (cdr lst)))))) 
0
source

The other answers in this thread seem too complicated (I read them all), so here I take it upon myself:

 (define (all-equal? lst) (define item (car lst)) (let next ((lst (cdr lst))) (cond ((null? lst) #t) ((equal? item (car lst)) (next (cdr lst))) (else #f)))) 

(It does not work with an empty list by design. If necessary, it is easy to add (if (null? lst) #t ...) ).

0
source

Short, short solution:

 #lang racket (define (all-equal? lst) (for/and ([i (in-permutations lst)]) (equal? (first i) (second i)))) ; TEST CASES (require rackunit) (check-false (all-equal? '(1 2 3))) (check-true (all-equal? '(1 1 1))) (check-true (all-equal? '())) 

Note that this uses racket, so this may not work with your schema implementation.

0
source

For it is bad in these languages. Try

 (define list-equal? (lambda (lst) (if (= lst null) (true) (foldr = (car lst) (cdr lst)) ))) 
-1
source

All Articles