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.224.26.198 with SMTP id f6mr667004qac.3.1348668584399;
        Wed, 26 Sep 2012 07:09:44 -0700 (PDT)
Received: by 10.236.183.101 with SMTP id p65mr114193yhm.20.1348668584377; Wed,
 26 Sep 2012 07:09:44 -0700 (PDT)
Path: e10ni53868478qan.0!nntp.google.com!l8no11590971qao.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups: comp.lang.lisp
Date: Wed, 26 Sep 2012 07:09:44 -0700 (PDT)
In-Reply-To: <acec9jF476bU1@mid.individual.net>
Complaints-To: groups-abuse@google.com
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=192.138.178.130;
 posting-account=AvekzAoAAABj-TclKcOWQmXwA49MFPGX
NNTP-Posting-Host: 192.138.178.130
References: <4b301750-d684-4467-91bd-ab9207d5d382@googlegroups.com>
 <ace6ovF2rhhU1@mid.individual.net> <20120925110438.405@kylheku.com> <acec9jF476bU1@mid.individual.net>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <01359518-4544-48e4-bb6d-76b764e39043@googlegroups.com>
Subject: Re: Trying to make my own reverse function
From: "Yves S. Garret" <yoursurrogate...@gmail.com>
Injection-Date: Wed, 26 Sep 2012 14:09:44 +0000
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Tuesday, September 25, 2012 2:43:00 PM UTC-4, Nils M Holm wrote:
> In article <20120925110438....@kylheku.com> you wrote:
>=20
> > If you recognize only the empty list as a base case, and do not recogni=
ze
>=20
> > the length one list as a base case, then you have infinite recursion. =
=20
>=20
>=20
>=20
> Well, I beg to differ:
>=20
>=20
>=20
> (DEFUN R (L)
>=20
>   (IF (NULL L)
>=20
>       NIL
>=20
>       (APPEND (R (CDR L)) (LIST (CAR L)))))
>=20
>=20
>=20
> (R '(1 2 3)) =3D=3D> (3 2 1)
>=20
> (R '(1))     =3D=3D> (1)
>=20
> (R '())      =3D=3D> NIL
>=20
>=20
>=20
> > But the first two cases can be merged since they return the
>=20
> > same thing and if (null l) is true then (null (rest l)) is also valid
>=20
> > and true
>=20
> > true (since Lisp is not S**eme, dammit!)
>=20
>=20
>=20
> And it works fine in Scheme, too.
>=20
>=20
>=20
> --=20
>=20
> Nils M Holm  < n m h @ t 3 x . o r g >  www.t3x.org

Perhaps I'm needlessly torturing myself in my learning process, but I remem=
ber one of my professors teaching me Scheme (many moons ago) and the first =
homework assignment was the reverse method without using append as well.  I=
 was trying to replicate the same thing here as well.

Long story short, this is my code and the error that I'm getting below when=
 trying to load it in Common Lisp.


; 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 fi=
rst
  ;   list will store the reversed list -- after processing is done, but fo=
r
  ;   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))))

; 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))))))))


The error:



[110]> (load "test.lisp")
;; Loading file test.lisp ...
*** - SYSTEM::%EXPAND-FORM: (LIST 'NIL LIST-TO-REVERSE) should be a lambda =
expression
The following restarts are available:
SKIP           :R1      skip (DEFUN CUSTOM-REVERSE2 # ...)
STOP           :R2      stop loading file /home/ashvets/Documents/Developme=
nt/Lisp/hello-world/test.lisp
ABORT          :R3      Abort main loop


So I can't pass in a nil into reverse-rec?