As has been pointed, the reverse
procedure returns a new list, it doesn't modify in-place the list that was passed as a parameter (this happens with all the procedures that modify lists, be careful with it). That's the reason why the line (reverse w)
in your code doesn't have any effect: surely it returns a new reversed list, but you ignored the value that was returned!
For using the reversed list, you have to store or pass it along somewhere, a good idea would be to save it in a local variable defined inside a let
form and refer to the variable from that point on. This is what @michaelb958 is suggesting:
(define key
(lambda (w)
(let ((w (reverse w)))
(if (null? w)
0
(let ((k 33))
(+ (* (ctv (car w)) k) (key (cdr w))))))))
However, be warned that the above will reverse the w
list each time the recursion is called as it traverses the list, I don't think that's what you intend! if you need to reverse the list only once, call reverse
before calling key
, and don't ever call reverse
inside key
:
(key (reverse '(x y z)))