I need to write a function that determines if the given list is a pair of elements. The program will simply respond #t if the list contains exactly two elements or #f if it does not, such that:
(zipper? '((a 1)(b 2))) => #t
and
(zipper? '((foo 100)(bar 2 3))) => #f
I'm still fairly new to Scheme so any help would be much appreciated! Thanks!
It isn't clear if the "correct" input for the procedure is an arbitrary list or a two-element list. If it's strictly a two-element list, this will work:
… And if it's an arbitrary-length list whose elements we want to check, this will work in Racket, using
andmap
:If you are not using Racket, then this solution using
every
will work in any interpreter with SRFIs:Either way, notice that the trick was defining the
is-two-element-list?
procedure, which verifies the two-element-list property, after that we can apply it as needed.