I am working on a Scheme function and would like to check if I have found the best solution for a specific subpart. Somehow it looks more complicated than necessary.
The function needs to test if each element of a given list is a (sub)list with exactly two elements. So
'((1 2)(3 4))
would return #t while
'((5 6)(7 8 9))
would return #f.
My solution is
#(define-scheme-function (parser location lst) (list?)
(if (memv #f (map (lambda sig
(and (list? (car sig))
(= 2 (length (car sig))))) lst))
\validate #'((1 2)(3 4))
\validate #'((5 6)(7 8 9))
The "framework" doesn't matter, it's just a compilable example. My question is only about the "if" _expression_.
What it does is:
- go through the elements of lst
- produce a list of boolean values,
- #t if we have a two element list,
- #f if not
- check if this intermediate list contains at least one #f
Somehow this looks clumsy to me, and I'd like to know (and learn) if there's a better solution for this.