Trying to rotate a list left in scheme/racket. For example: (rotate-left '(abc)) outputs (bca)
Here's ze code!
(define (rotate-left LIST)
(if(null? LIST)'()
(cons(car LIST)(cons(cdr LIST)'())))))
Unfortunately this just outputs the same list! Should be an easy fix I'm sure, and I know I'm close...Thanks for any help !
The answer given by @laser_wizard doesn't work ... Here's a correct implementation - you have to replace the first cons
with an append
for adding the first element at the end of the list.
(define (rotate-left LIST)
(if (null? LIST)
'()
(append (cdr LIST)
(cons (car LIST)
'()))))
(rotate-left '(a b c))
> '(b c a)
Test cases! Test cases! A check-expect here would have helped you understand the problem, and others to understand what you were looking for.
I think I'm getting old.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With