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)
Date: Wed, 26 Sep 2012 07:09:44 -0700 (PDT)
Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=22.214.171.124;
<ace6ovF2rhhU1@mid.individual.net> <email@example.com> <acec9jF476bU1@mid.individual.net>
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
On Tuesday, September 25, 2012 2:43:00 PM UTC-4, Nils M Holm wrote:
> In article <20120925110438....@kylheku.com> you wrote:
> > If you recognize only the empty list as a base case, and do not recogni=
> > the length one list as a base case, then you have infinite recursion. =
> Well, I beg to differ:
> (DEFUN R (L)
> (IF (NULL L)
> (APPEND (R (CDR L)) (LIST (CAR L)))))
> (R '(1 2 3)) =3D=3D> (3 2 1)
> (R '(1)) =3D=3D> (1)
> (R '()) =3D=3D> NIL
> > But the first two cases can be merged since they return the
> > same thing and if (null l) is true then (null (rest l)) is also valid
> > and true
> > true (since Lisp is not S**eme, dammit!)
> And it works fine in Scheme, too.
> 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=
; set things up here so that we will pass in a list of two lists. The fi=
; list will store the reversed list -- after processing is done, but fo=
; the moment is blank -- and the second list is the one that was passed=
; in order to reverse.
(reverse-rec((list '() list-to-reverse))))
; this method will do the iterating and reversing recursively.
; 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.
; 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))))))))
> (load "test.lisp")
;; Loading file test.lisp ...
*** - SYSTEM::%EXPAND-FORM: (LIST 'NIL LIST-TO-REVERSE) should be a lambda =
The following restarts are available:
SKIP :R1 skip (DEFUN CUSTOM-REVERSE2 # ...)
STOP :R2 stop loading file /home/ashvets/Documents/Developme=
ABORT :R3 Abort main loop
So I can't pass in a nil into reverse-rec?