Code: Select all
(define (length? lst (n 1)(op =) booltest)
(and (op (length lst)
(if (or (string? n)(list? n))(length n) n))
(if booltest
(if (list? booltest)
(for-all true?
(map for-all booltest (dup lst (length booltest))))
(for-all booltest lst))
true
)))
(length? lst) - does the list have 1 element?
(length? lst 2) - does the list have 2 elements?
(length? lst 3 >) - does the list have more than 3 elements?
(length? lst 3 = integer?) - do we have a list of 3 integers?
(length? lst 3 = (list integer? zero?)) - do we have a list of 3 integers that are 0?
That is pretty neat but there is more! If we have another list instead of a comparison number we can use a comparison operator to see how the number of arguments compare with one another. In this case we can only use <, = and > as comparators. Here are some more examples of how this works:
(length? lst1 lst2) - are the lists of equal length?
(length? lst1 lst2 >) - does lst1 have more arguments than lst2?
(length? lst1 lst2 <) - does lst1 have fewer arguments than lst2?
Of course one can compare string lengths this way as well. I think it is clear that this makes life much easier. Here is an example of how one could use the function in a practical way. The integer-vector? function determines if its input is a non-zero length list of integers.
Code: Select all
(define (integer-vector? v)(length? v 0 > integer?))