views:

101

answers:

1

Hey, I'm trying to use and in a cond statement. Basically, instead of simply checking that <exp1> is true before running some code, I need Scheme to check that <exp1> AND <exp2> are true. I understand that (and #t #f) evaluates to #f and that (and (= 10 (* 2 5)) #t) evaluates to #t. Unfortunately, Scheme will not accept

(and (eqv? (length x) 1) (eqv? (car x) #t))

where x is a list whose first element is an S-expression that evaluates to either #t or #f (in fact, I wanted to just do (and (eqv? (length x) 1) (car x)), but that didn't work).

Can anyone explain what I am doing wrong, or how to fix it? On a side note, does anyone know what ... means in Scheme, if anything? Thanks!

+2  A: 

"where x is a list whose first element is an S-expression that evaluates to either #t or #f (in fact, I wanted to just do (and (eqv? (length x) 1) (car x)), but that didn't work."

In the second case of (car x), you just get the first element in the list, you do not evaluate it.

Say your list x is a list ((eq? 3 3)), I'm just saying something. Its length is 1, and it's first element evaluates to #t if we evaluate it, but (car x) in this case retrieves a list (eq? 3 3), which is a list of one symbol and two numbers.

The solution to your problem would be using eval, as in (eval (car x) (null-environment)), which evaluates a datum, such as a list.

If you use ((car x)) as noted in another answer, this will only work if your first element of a list is a thunk, a nullary, if you constructed x by evaluating the list (list (lambda () #t)).

In that case, your first element would be a function which when called with no arguments yields #t.

Lajla