Again, two different implementations to help me in thinking about the mechanics of list manipulations.
(define (reverse-ls xs) (define (reverse-loop items result) (cond ((null? items) result) (else (reverse-loop (cdr items) (cons (car items) result))))) (reverse-loop xs nil)) (define (reverse-ls xs) (append (reverse (cdr xs)) (list (car xs))))
Update:
As tonghu pointed out in the comments, the 2nd version fails on null input. I was too hasty making two version that I didn’t test them both!
(define (reverse-ls-2 xs) (cond ((null? xs) null) (else (append (reverse (cdr xs)) (list (car xs))))))
Can you test it, I think may be it’s wrong
You’re right, thanks!
I didn’t check the input in the second version.
Thank you for these awesome solutions. They helped me a lot.
Is there any online environment for executing Lisp. If not, id there a simple guide for setting up Lisp environment on Mac?
I don’t know about a suitable online environments, but Racket (https://racket-lang.org/) and DrRacket (https://docs.racket-lang.org/drracket/index.html) are definitely suitable offline and support MacOS. There is even an SICP language (https://docs.racket-lang.org/sicp-manual/SICP_Language.html) that provides support for working through the book.