Reputation: 187
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:
What I don't know:
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
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
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