His first try produces the reverse list. Why?
Well, he wrote an internal recurring function which takes the unprocessed (unsquared) list as its first argument
and accumulates the result in the second argument. The accumulation process takes the square of the first argument
of the unprocessed list and conses it with the already-processed result.
Now lets examine a simple case of list-square'ing a [1 2] vector:
1. things = [1 2], answer = nil
2. things = [2], answer = [1] : nil
3. things = nil, answer = [4] : [1] : nil
Second try isn't much better. Lets see what happens:
1. things = [1 2], answer = nil
2. things = [2], answer = nil : [1]
This shouldn't even work, as (cons nil x) doesn't make any sense.
| ( defn square-list-2 [ xs ]
( letfn [ ( iter [ things answer ]
( if ( empty? things )
answer
( recur ( rest things )
( cons answer ( sqr ( first things ) ) ) ) ) ) ]
( iter xs nil ) ) )
|
|