Quantcast
Channel: Scheme: How to check if all elements of a list are identical - Stack Overflow
Viewing all articles
Browse latest Browse all 11

Scheme: How to check if all elements of a list are identical

$
0
0

I'd like to create a Scheme function that yields true if it is passed a list that is composed entirely of identical elements. Such a list would be '(1 1 1 1). It would yield false 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)      )

Clearly this is incorrect, and I'm new to this. I guess I'm unable to express the step where I'm supposed to return #t or #f.

Thanks in advance!

EDIT: I fiddled a bit and found a solution that seems to work very well, and with a minimal amount of code:

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

Thanks again for the help everyone.


Viewing all articles
Browse latest Browse all 11

Latest Images

Trending Articles





Latest Images