One of the first questions in the second chapter of The Little Schemer (4th edition) asks the reader to write the function lat?, where (lat? l) returns true if l is a list of atoms.
It goes on to say:
You were not expected to be able to do this yet, because you are still missing some ingredients.
But I'm familiar with recursion, and the definition of atom? earlier in the book already introduced and (further implying the existence of or), so I gave it a shot anyway: (repl)
(define lat?
(lambda (l)
(or
(null? l)
(and
(atom? (car l))
(lat? (cdr l))))))
On the next page, the book introduces the cond operator to enable this definition of lat?:
(define lat?
(lambda (l)
(cond
((null? l) #t)
((atom? (car l)) (lat? (cdr l)))
(else #f))))
Is there any significant difference between these two implementations?
condis a special form, that takes (roughly) the formIts semantics is that it will evaluate the
test-expressions in order, and for the first one that it finds to evaluate to#t(thetruevalue), then it will evaluate its associatedthen-expressionand return its value. If all thetest-expressions evaluate to#f(thefalsevalue), and anelseclause is present, then it will evaluate its associatedthen-expression3in this case, and return its value.So as far as semantics are concerned, the two implementations are equivalent. Their only difference might be that afaik the
condversion is considered more idiomatic in the Scheme community.