Re: [racket-users] Little Schemer?

The Little Schemer is an excellent choice to learn recursion :)

'or' evaluates the expressions from left to right. It returns #f, if none of its expressions returns #t. If one of its expressions returns #t, 'or' returns the result of this expression without evaluating the rest of the expressions.

Some examples:
(or) => #f
(or #f #f #f #f) => #f
(or #f #f "scheme" "is" "fun") => "scheme"
(or #f (+ 1 2)) => 3
(or #f "scheme" (+ 1 2)) => "scheme"

Every expression which is not #f or does not return #f returns #t in control operators.

Some examples:
(if "hallo" "world" "no") => "world"
(if (+ 1 2) "yes" "no") => "yes"

Here is the definition of member*:

(define member*
(lambda (a l)
(cond
((null? l) #f)
((atom? (car l))
(or (eq? (car l) a) (member* a (cdr l))))
(else (or (member* a (car l)) (member* a (cdr l)))))))

When evaluating (member* 'd '(a bc d ((e)))), here is what happens:

1. (null? '(a bc d ((e)))) => #f
2. (atom? (car '(a bc d ((e))))) => #t
3. (or (eq? 'a 'd) (member* 'd (cdr '(a bc d ((e))))))
4. (eq? 'a 'd) => #f
5. (member* 'd '(bc d ((e))))
6. (null? '(bc d ((e)))) => #f
7. (atom? (car '(bc d ((e))))) => #t
8. (or (eq? 'bc 'd) (member* 'd (cdr '(bc d ((e))))))
9. (eq? 'bc 'd) => #f
10. (member* 'd '(d ((e))))
11. (null? '(d ((e)))) => #f
12. (atom? (car '(d ((e))))) => #t
13. (or (eq? 'd 'd) (member* 'd (cdr '(d ((e))))))
14. (eq? 'd 'd) => #t

So (eq? 'd 'd) returns #t, which is the return value of (member* 'd '(a bc d ((e)))) - (member* 'd (cdr '(d ((e))))) is never evaluated. Here is a version without 'or':

(define member*
(lambda (arg l)
(cond
((null? l) #f)
((atom? (car l))
(if (eq? (car l) arg)
#t
(member* arg (cdr l))))
(else
(if (member* arg (car l))
#t
(member* arg (cdr l)))))))

--
You received this message because you are subscribed to the Google Groups "Racket
Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email