<< Previous exercise (3.13) | Index | Next exercise (3.15) >>
Call : (mystery '(a b c d)) :
+------------------------------------------------------------------------+ | x: . | | y: / | | +------------------------------------------------------------------------+ | +----------------+ | | temp: . | | +------- |-------+ | | | | -------- ------------------------------ v v +---+---+ +---+---+ +---+---+ +---+---- | . | .------->| . | .------->| . | .------->| . | / | +-|-+---+ +---+---+ +-|-+---+ +-|-+---+ v v v v +---+ +---+ +---+ +---+ | a | | b | | c | | d | +---+ +---+ +---+ +---+
Result after 1 cycle through loop
+------------------------------------------------------------------------+ | x: .---------------------- | | y: . | | +----------------------------------|-------------------------------------+ | | +----------------+ | | | temp: . | | | +--------|-------+ | | | | | | -------- | |--------------- v v v +---+---+ +---+---+ +---+---+ +---+---- | . | / | . | .------->| . | .------->| . | / | +-|-+---+ +---+---+ +-|-+---+ +-|-+---+ v v v v +---+ +---+ +---+ +---+ | a | | b | | c | | d | +---+ +---+ +---+ +---+
Result after 2 cycle
+------------------------------------------------------------------------+ | x: .------------------------------------- | | y: . | | +-------------------------------------------------|----------------------+ | | +----------------+ | | | temp: . | | | +--------|-------+ | | | | | | -----------------------| | | | | | | | | ---------------------- | | v v | v v +---+---+ +---+---+ | +---+---+ +---+---- | . | / | . | .---- | . | .------->| . | / | +-|-+---+ +---+---+ +-|-+---+ +-|-+---+ v v v v +---+ +---+ +---+ +---+ | a | | b | | c | | d | +---+ +---+ +---+ +---+
Result after 3 cycle
+------------------------------------------------------------------------+ | x: .----------------------------------------- | | y: . | | +-----------------------------------------------------|------------------+ | | +----------------+ | | | temp: / | | | +----------------+ | | | | -------------------------------------- ----------- | | ----- -------------- -- | | | | | ---------------------- | | | v v | v | v +---+---+ +---+---+ | +---+---+ | +---+---- | . | / | . | .---- | . | .---- | . | / | +-|-+---+ +---+---+ +-|-+---+ +-|-+---+ v v v v +---+ +---+ +---+ +---+ | a | | b | | c | | d | +---+ +---+ +---+ +---+
Result after 4 cycle
+------------------------------------------------------------------------+ | x: / | | y: . | +------------------------------------------------------------------------+ | +----------------+ | | temp: / | | +----------------+ | | -----------------------------------------------------| --------------------- | | | | | | ----- -------------- -- | | | | | | | ---------------------- | | | | v v | v | v | +---+---+ +---+---+ | +---+---+ | +---+---+ | | . | / | . | .---- | . | .---- | . | .----| +-|-+---+ +---+---+ +-|-+---+ +-|-+---+ v v v v +---+ +---+ +---+ +---+ | a | | b | | c | | d | +---+ +---+ +---+ +---+
Result after 5 cycle
Printed value : w : (d c b a)
meteorgan is wrong. The interpreter just made a shallow copy when it passed mystery parameters, so statement “(set-cdr! x y)” affects the value of v.
v => (a) w => (d c b a)
Apologies if diagram is unclear.
Below is the final box and pointer diagram after the procedure mystery is called.
W will point at the box pointing at d, whose cdr will point at the box pointing at c, so on and so forth until it reaches the box pointing at a, whose cdr points at '(). Therefore, W is now the reversed order of V.
V still points at the box pointing at a, but the cdr of a was set to be '() in the first iteration of loop. Therefore, V is now (a).