0
(define (search-for-primes start end)
   (if (even? start)
       (search-for-primes (+ start 1) end)
       (cond ((< start end) (timed-prime-test start)
                        (search-for-primes (+ start 2) end)))))

This is part of an answer for SICP exercise 1.22 (see link at the bottom). Why is it that in the above code the guy is able to put two things after the cond condition ( (< start end) )? How does this manage to work?

If I even do (cond ((< 4 5) (< 4 3) (< 6 7))) in the terminal then that brings an error.

http://www.billthelizard.blogspot.com/2010/02/sicp-exercise-122-timed-prime-test.html

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
ABP123
  • 3
  • 1

1 Answers1

3

In cond, after each condition there's an implicit begin, so you can write any number of expressions afterwards, but only the value of the last one is returned as a value of that condition. In fact, your example works:

(cond ((< 4 5) (< 4 3) (< 6 7)))
=> #t

The above is equivalent to:

(cond ((< 4 5)
       (begin
         (< 4 3)
         (< 6 7))))

What happened there? The condition (< 4 5) was evaluated to #t, then (< 4 3) was evaluated (but the value is lost, you didn't do anything with it) and finally the expression (< 6 7) was evaluated and its result returned: #t.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thank you. This confused me because it seems that there is no situation in SICP so far where the program "progresses" (how I think of it) without a function being "passed through". – ABP123 Mar 08 '16 at 15:10
  • All the expressions in the `begin` get executed for the _effect_ (say, a `set!` or a `display`). Only the last one gets executed for the _value_. That's why you have not seen such a thing in the book up until this point, the authors have not yet introduced the notion of stateful operations. – Óscar López Mar 08 '16 at 15:12
  • pedagogical note: the example could not be more confusing: unmotivated comparisions, with variations difficult to spot, and no reason to be. – Str. Mar 08 '16 at 17:27
  • @Str.In Óscars defense it was the OPs example and not his. – Sylwester Mar 08 '16 at 21:06