I'm struggling with something. Let me start with code for the
rember function then ask. I'm asked to find the value of (
rember a lat).
CODE
(define rember
(lambda (a lat)
(cond
((null? lat) (quote ()))
((eq? (car lat) a) (cdr lat))
(else (cons (car lat)
(rember a (cdr lat)))))))
where argument:
a is
andand
lat is (
bacon lettuce and tomato)
I'm working from "
The Little Schemer" by Friedman and Felleisen. I understand that the function
rember is checking each first atom (
car) of the list of atoms (
lat) in sequence, looking for identity with the argument
a. Failing that, it squirrels away these non-
a atoms to be constructed (
cons) back onto the remaining
lat once a is found and dispensed with (
rember).
Here's my confusion and it has to do with the
recursive behaviour in rebuilding my list once deconstructed.
1. By the time the function
rember finds
a,
lat equals (
rember a (
cdr lat)) equals
tomato.
2. Affixing
car to the front of it with
cons redefines
lat to equal (
lettuce tomato).
3.
Where is bacon? Where does my function
rember find it and
cons it to
lat?
**3a. As (cdr lat) redefined
lat from (
bacon lettuce and tomato) to(
lettuce and tomato) to (
and tomato),
car was simultaneously redefined from
bacon to
lettuce.
In a nutshell: what happened to
bacon?
thx
This post has been edited by LowWaterMark: 28 Sep, 2008 - 09:53 PM