[racket] can't cdr list from dotted-tail notation?
I already spent a couple of hours on this, hope I am not missing something really obvious - does the dotted notation produce some kind of flat list instead of cons cells?
(define list-rf (lambda ( n . items )
(if (= n 0)
(car items)
(list-rf (- n 1)(cdr items)))))
(list-rf 3 1 4 9 16 25 1 4 9 16 25 1 4 9 16 25 1 4 9 16 25)
output: '()
If I try to end the recursion on (null? items) it goes into an infinite loop
(define test2 (lambda y
(if (or(null? y)(null? (cdr y)))
y
(printf"~a~n"(cons (car y) (test2 (cdr y)))))))
(test2 1 2 3 4 5 6 7);⇒ <i>(1 3 5 7)</i>
(trace test2)
output:
{2 {3 4 5 6 7}}
{1 {2 3 4 5 6 7}}
and test2 doesn't recur. If I replace that (if (or (null ...
with
(if (null? y)
one last test
(define test (lambda y
(if(begin
(printf"~a:list ~a:null ~a:len ~n"(list? y)(null? y)(length y))
(printf"~a:~n"y)
(null? y))
(printf"done")
(if(> (car y) 9)
#f
(begin
(printf"~a:~n"y)
(test (cdr y)))))))
(test 2 3 4 5 6 7 2 3 4 5 6 7 2 3 4 5 6 7 999);
output: how does it go from length 17 to length 1 with one cdr?
#t:list #f:null 19:len
{2 3 4 5 6 7 2 3 4 5 6 7 2 3 4 5 6 7 999}:
{2 3 4 5 6 7 2 3 4 5 6 7 2 3 4 5 6 7 999}:
#t:list #f:null 1:len
{{3 4 5 6 7 2 3 4 5 6 7 2 3 4 5 6 7 999}}:
. . >: contract violation
expected: real?
given: (mcons 3 (mcons 4 (mcons 5 (mcons 6 (mcons 7 (mcons 2 (mcons 3 (mcons 4 (mcons 5 (mcons 6 (mcons 7 (mcons 2 (mcons 3 (mcons 4 (mcons 5 (mcons 6 (mcons 7 (mcons 999 '()))))))))))))))))))
argument position: 1st
other arguments...:
9
>
the prompt never returns.
it's confusing the heck out of me.