Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find whether element in list integer?

Tags:

scheme

I was wondering, how do you check if every element in a list is an integer or not? I can check the first element by using (integer? (car list), but if I do (integer? (cdr list), it always returns false (#f) because the whole of the last part of the list is not an integer as a group. In this case let's say list is defined as. (define list '(1 2 5 4 5 3))

like image 224
Adeq Hero Avatar asked Feb 20 '23 18:02

Adeq Hero


2 Answers

  (define get-integers
    (lambda (x)
     (if (null? x)
        "All elements of list are integers"
        (if (integer? (car x))
            (get-integers (cdr x))
            "Not all elements are an integer"))))
like image 96
ewein Avatar answered Feb 23 '23 08:02

ewein


Practical Schemes provide functions for doing tests across whole sequences. An application of the andmap function, for example, would be appropriate. Racket provides a for/and to do something similar. If you really needed to write out the loop by hand, you'll be using recursion.

like image 39
dyoo Avatar answered Feb 23 '23 08:02

dyoo