Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
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)
Path: g9ni20614pbh.1!nntp.google.com!border1.nntp.dca.giganews.com!border4.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!novia!news-hub.siol.net!news.mi.ras.ru!goblin-spool!goblin1!goblin.stu.neva.ru!news.glorb.com!news-in-01.newsfeed.easynews.com!easynews!core-easynews-01!easynews.com!en-nntp-11.dc1.easynews.com.POSTED!not-for-mail
From: Barry Fishman <barry_fish...@acm.org>
Newsgroups: comp.lang.lisp
Subject: Re: Trying to make my own reverse function
References: <4b301750-d684-4467-91bd-ab9207d5d382@googlegroups.com>
	<ace6ovF2rhhU1@mid.individual.net> <20120925110438.405@kylheku.com>
	<acec9jF476bU1@mid.individual.net>
	<01359518-4544-48e4-bb6d-76b764e39043@googlegroups.com>
Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAALVBMVEXG87t8xXThBQWq85q9
 87AvUC6PUVH/BgamyajC87a/87P////r6+ud7oq49KsBy7dJAAACKUlEQVQ4jc3Sv2vbQBQH
 8CulwcEdeoOKwM1QD/bSzVktKDEdMpRqeMKQFNqAhEGbh3aVB5sDafAYL106xZMzuAiehnqI
 EciLMR2viz0Vor+hdydZMa6z97sI9NH7cYdI9ZGQr4/kP4Fu1akehFqHDaoHoOviPLD3oVtz
 7PIErFCeeRdqOuvptwDMYSyfpKB7iYh3DOCTeOKzegGvk8hD1O22eD2B9tUWum9wbpXYy6NA
 wAzMeQFu1AeT2VAW0Af4sYUvfmRbCWLciXq0B2ZYf4C+GL/AGBIcOP4O4MzFKbiTAGOPYg7V
 mu1j5OIdfIgT7FtHGKoZIHKB6IuK9kDODpNsK0pPVnqCFbyFSyYWMMPnIA9CVsbm5l2HuRjZ
 FVERBXFPs8QQMhIZvqUX8jJmiTzIRy5FwWhEdAmR4y1KnQbn1KrnMOrLj6PPDnW8bw2+1K7I
 OINrecF9jwyJyBlfAvkzvlEy6DBHJ3m+8yU5HY9V0TWl+nALx+slMQTkRWKHQprEOJeST3qg
 p8RoHZRjsmm1ctrrJ0HkX0mFvG+uTnf7qe3SNG2dcc4bm70iAemaHxAJ91zF2G2noMl/UVVk
 bAoihgINlKwa6XkuhBsKGJgnPBuVEeG/5Wzqz3tZ0bYh4SsBS+qXZtTMRTWUcC/Bnybi7+R5
 VoaAdZNz7ZU/RT8u2nEuQTw0VpmjH/7UChHQlFAGT1Rg8GS7Hcm+0F4EDgvYIohLAFS++wvs
 R0Pau3fdJgAAAABJRU5ErkJggg==
Message-ID: <m3mx0clq58.fsf@barry_fishman.acm.org>
User-Agent: Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.2.50 (gnu/linux)
Cancel-Lock: sha1:SXoj78uSqdLYIFiRyx/OrhUUkus=
MIME-Version: 1.0
Lines: 78
X-Complaints-To: abuse@easynews.com
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
X-Received-Bytes: 4783
Bytes: 4944
Content-Type: text/plain


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
or 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)
        to-list
        (reverse-rec (list (cons (car from-list) to-list)
                           (cdr from-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))

Then:

(defun reverse-rec (to-list from-list)
    (if (null from-list)
        to-list
        (reverse-rec (cons (car from-list) to-list)
                     (cdr from-list))))

-- 
Barry Fishman