usernvk
usernvk

Reputation: 187

Simply Scheme Exercises. Chapter 06 True and False

Greets, I'm new to SO so please take care of me.

Exercise 6.1 in Simply Scheme has the expression:

(cond (empty? 3)
      (square 7)
      (else 9))

My mind says it should evaluate (square 7) and return that. (empty? 3) is evaluated instead (returned the atom — tried it with other atoms and lists, same deal).

I'm confused.

What I know:

  1. Everything in Scheme that is not false is true. If 3 is true and is not empty (#f), why does the cond expression return (empty? 3)?
  2. The first argument to a cond expression is evaluated and where it is true, returns #t, the value defined or #undefined dependent on context. If false, it continues evaluating the cond arguments successively until it does so (or finds no suitable return value) then exits the cond.

What I don't know:

  1. (empty? 3) on its own returns #f. Why does the cond terminate here and not evaluate (square 7)?
  2. Why does the evaluation of (empty? 3) within the cond return the atom, not #t or #f?

I am using SCM with Slib and the additional libraries supplied with Simply Scheme (simply.scm, functions.scm, ttt.scm, match.scm, database.scm) loaded.

The empty? definition in simply.scm is beyond my scheme grasp at this point.

Upvotes: 1

Views: 450

Answers (2)

WorBlux
WorBlux

Reputation: 1413

That's not quite what cond does. cond accepts one or more arguments, each argument which must be a list of scheme-expressions.

(cond (#t)) is a valid cond statement.

It evaluates the first expression, and if true, evaluates as many additional s-epressions in that list, and returns the value of the last expression evaluated.

(cond (#t 1 2 3 4 (if (number? 0) "Yeah, sanity!" "It rubs lotion on it's skin"))) is a valid cond statement

Upvotes: 1

Ankur
Ankur

Reputation: 33657

The cond form is like this:

(cond (something-to-check-for-truthiness value-if-truthiness-succeed)
        ...other checks in the same format as above  )

Now if you put your code into this format then. empty? i.e just the function empty (not its call) fits in place of something-to-check-for-truthiness and a function is always a truth value hence number 3 is returned which is after empty? and fits into value-if-truthiness-succeed slot. So, there is no call to empty?function at all.

Upvotes: 2

Related Questions