reverse a list

Posted on 2004-10-05
Last Modified: 2010-04-17
how to reverse a list in Lisp apart of using it's buid-in function reverse????

Question by:nguln
  • 3
LVL 84

Expert Comment

ID: 12224700
Is this a homework problem?
You probably want to use car cdr and cons

Expert Comment

ID: 12225406
please what is "list in lisp" ???

Expert Comment

ID: 12230495
LISP is a functional programming language designed to use lists.  Lists are defined by brackets as so:
( "a" 2 "c")
and embedded as so:
("a" (1 "b") ("c" ("d")))

which is a list with three elements, where the second element is a list with two elements, and the third element is a list with two elements, the second of which is a list.  Actually, only result lists display like that.  To define a list, use the List function, like this:

(List "a" 2 "c")

where you want the list elements to be the atoms "a", 2, and "c".

Accepted Solution

Rick_Townsend earned 20 total points
ID: 12230531
As for answering the posted question, ozo is right, use car to get the first element of the list, and cdr to get the rest of the list.  Build your result using cons, where the two parameters will be a recursive call to your reverse function and the head of the current list.

Expert Comment

ID: 12230543
The solution is elegant, but I think EE rules are that we can't give actual homework answers, only answer questions and help with debugging your algorithm.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
count8 challlenge 13 120
topping2 challenge 13 83
C Programming - If Statement 8 72
Help to convert powershell script into a gui 9 143
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
A short article about problems I had with the new location API and permissions in Marshmallow
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now