Message from discussion Trying to make my own reverse function
Received: by 10.66.75.39 with SMTP id z7mr1174336pav.26.1349309065111;
Wed, 03 Oct 2012 17:04:25 -0700 (PDT)
From: Barry Fishman <barry_fish...@acm.org>
Subject: Re: Trying to make my own reverse function
User-Agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.2.50 (gnu/linux)
Organization: EasyNews, UseNet made Easy!
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Wed, 26 Sep 2012 12:12:19 -0400
You are making thing less readable by not putting a space before (.
Lisp can become easy to read if you don't try to make it look like
some other language.
On 2012-09-26 10:09:44 EDT, Yves S. Garret wrote:
> ; this is the initial function that will set things up for the below method.
> (defun custom-reverse2(list-to-reverse)
> ; set things up here so that we will pass in a list of two lists. The first
> ; list will store the reversed list -- after processing is done, but for
> ; the moment is blank -- and the second list is the one that was passed in
> ; in order to reverse.
> (reverse-rec((list '() list-to-reverse))))
(reverse-rec (list '() list-to-reverse)))
The double parens are usually wrong unless you are using some
macro like let. (The lack of a space may be making it appear to be
a list inside a function call.)
> ; this method will do the iterating and reversing recursively.
> (defun reverse-rec(grand-list)
> ; get the second list in the grand-list and see if it's nil.
> (if (null (car (cdr grand-list)))
> ; if the second list is a nil, then return the reversed list.
> (car grand-list)
> ; otherwise continue reversing the entire thing.
> ((setf new-list (cons (car (car (cdr grand-list))) (car grand-list)))
> (reverse-rec(list new-list (cdr (car (cdr grand-list))))))))
First setf is not the way to create a local variable, you need to use let
Second, the '((setf ...', outside of a let, is another case of doubled
left parenthesis and a warning something may be wrong. Here you are
grouping two statements by putting them in an outer parenthesis:
((setf newlist ...) (reverse-rec ...))
You would do that with:
(progn (setf newlist ...) (reverse-rec ...))
Add considering that the setf is wrong:
(let ((newlist ...)) (reverse-rec ...))
Also duplicate chains of '(cdr (car ...' are a hint that you need to
choose more meaningful local variable:
;; This method will do the iterating and reversing recursively.
(defun reverse-rec (grand-list)
(let ((to-list (car grand-list))
(from-list (car (cdr grand-list))))
(if (null from-list)
(reverse-rec (list (cons (car from-list) to-list)
The '(car (cdr grand-list))' could be expressed as (cadr grand-list) or
even (second grand-list).
But would be simpler to rather than create a new grand-list as an
argument to reverse-rec, just pass the lists as separate arguments.
(defun custom-reverse2 (list-to-reverse)
(reverse-rec '() list-to-reverse))
(defun reverse-rec (to-list from-list)
(if (null from-list)
(reverse-rec (cons (car from-list) to-list)