| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| my reverse: (defun rvrs (lista) (if (null lista) '() (cons (rvrs (cdr lista)) (car lista)))) CL-USER> (rvrs '(1 2 3)) (((NIL . 3) . 2) . 1) built-in: CL-USER> (reverse '(1 2 3)) (3 2 1) So my reverse gets the spirit right but doesnt deliver exactly what I want. How do I cons without adding dots? |
|
#2
| |||
| |||
| ssecorp wrote: > my reverse: > (defun rvrs (lista) > (if (null lista) > '() > (cons (rvrs (cdr lista)) (car lista)))) > > CL-USER> (rvrs '(1 2 3)) > (((NIL . 3) . 2) . 1) > > built-in: > CL-USER> (reverse '(1 2 3)) > (3 2 1) > > So my reverse gets the spirit right but doesnt deliver exactly what I > want. How do I cons without adding dots? Do you know what a cons is, and how a proper list is defined in terms of conses? kt |
|
#3
| |||
| |||
| In article <186b2923-9d79-4385-bdfe-d3b6642dee04@t54g2000hsg.googlegroups.com>, ssecorp <circularfunc@gmail.com> wrote: > my reverse: > (defun rvrs (lista) > (if (null lista) > '() > (cons (rvrs (cdr lista)) (car lista)))) > > CL-USER> (rvrs '(1 2 3)) > (((NIL . 3) . 2) . 1) > > built-in: > CL-USER> (reverse '(1 2 3)) > (3 2 1) > > So my reverse gets the spirit right but doesnt deliver exactly what I > want. How do I cons without adding dots? Hint: The dot goes away when the cdr is another list. But in your case, the cdr is just a single element (the car of the original list). Look at the simplest case: (rvrs '(1)) == (cons (rvrs '()) 1) == (cons '() 1) Work on fixing this case, and I think the rest should fall into place. -- Barry Margolin, barmar@alum.mit.edu Arlington, MA *** PLEASE post questions in newsgroups, not directly to me *** *** PLEASE don't copy me on replies, I'll read them in the group *** |
|
#4
| |||
| |||
| ssecorp wrote: > my reverse: > (defun rvrs (lista) > (if (null lista) > '() > (cons (rvrs (cdr lista)) (car lista)))) > > CL-USER> (rvrs '(1 2 3)) > (((NIL . 3) . 2) . 1) > > built-in: > CL-USER> (reverse '(1 2 3)) > (3 2 1) > > So my reverse gets the spirit right but doesnt deliver exactly what I > want. How do I cons without adding dots? One idea would be to implement (a slightly modified) useful Haskell function like this one: (defun foldl (fun id list) (if list (foldl fun (funcall fun (car list) id) (cdr list)) id)) The idea of this function is to calculate for a list (1 2 3) the result (fun 3 (fun 2 (fun 1 id))) Now your rvrs function can be written like this: (defun rvrs (list) (foldl #'cons nil list)) With foldl some other nice functions are easy, e.g.: (defun sum (list) (foldl #'+ 0 list)) Of course, Common Lisp doesn't guarantee to be tail recursive, so for long lists you would use something like this: (defun rvrs (list) (loop for i in list with result = nil do (push i result) finally (return result))) And for sum you would just write (reduce #'+ list). -- Frank Buss, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de |
|
#5
| |||
| |||
| On Aug 23, 6:55*pm, Kenny <kentil...@gmail.com> wrote: > ssecorp wrote: > > my reverse: > > (defun rvrs (lista) > > * (if (null lista) > > * * * '() > > * * * (cons (rvrs (cdr lista)) (car lista)))) > > > CL-USER> (rvrs '(1 2 3)) > > (((NIL . 3) . 2) . 1) > > > built-in: > > CL-USER> (reverse '(1 2 3)) > > (3 2 1) > > > So my reverse gets the spirit right but doesnt deliver exactly what I > > want. How do I cons without adding dots? > > Do you know what a cons is, and how a proper list is defined in terms of > conses? > > kt yes so I tried: (defun rvrs (lista) (if (null lista) '() (list (car (rvrs (cdr lista))) (car lista)))) (defun rvrs (lista) (if (null lista) '() (list (rvrs (cdr lista)) (car lista)))) |
|
#6
| |||
| |||
| thanks frank I see know. but Im still trying to figure out how to do it with just one simple reucrsive function just as an exercise. (defun rvrs (lista) (if (null lista) '() (cons (rvrs (cdr lista)) (list (car lista))))) CL-USER> (rvrs '(1 2 3)) (((NIL 3) 2) 1) |
|
#7
| |||
| |||
| can I accumulate a list with loop? like so: CL-USER> (mapcar (lambda (x)(* x x)) (range 1 10 2)) (1 9 25 49 81) with loop? CL-USER> (loop for i in (range 1 10 2) do (print (* i i i))) 1 27 125 343 729 NIL (loop for i in (range 1 10 2) do (* i i i)) NIL |
|
#8
| |||
| |||
| ssecorp wrote: > can I accumulate a list with loop? > > like so: > CL-USER> (mapcar (lambda (x)(* x x)) (range 1 10 2)) > (1 9 25 49 81) > > > with loop? > CL-USER> (loop for i in (range 1 10 2) do (print (* i i i))) I don't know range, but assume it is something like this: (defun range (start emd step) (loop for i from start to end by step collect i)) And this is the answer to your question, if you can accumulate with loop. -- Frank Buss, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de |
|
#9
| |||
| |||
| ssecorp <circularfunc@gmail.com> writes: > thanks frank I see know. > > but Im still trying to figure out how to do it with just one simple > reucrsive function just as an exercise. > > (defun rvrs (lista) > (if (null lista) > '() > (cons (rvrs (cdr lista)) (list (car lista))))) The error is that you call CONS with a list instead of with an element as the first argument. A list is either the symbol NIL, representing the empty list (), or a CONS cell containing an element, followed a LIST containing the rest. LIST --> NIL LIST --> (CONS element LIST) If you try to use CONS otherwise, you won't get a LIST, but a different data stucture, like a TREE or whatever. > CL-USER> (rvrs '(1 2 3)) > (((NIL 3) 2) 1) -- __Pascal Bourguignon__ http://www.informatimago.com/ Kitty like plastic. Confuses for litter box. Don't leave tarp around. |
|
#10
| |||
| |||
| ssecorp wrote: > thanks frank I see know. > > but Im still trying to figure out how to do it with just one simple > reucrsive function just as an exercise. > > (defun rvrs (lista) > (if (null lista) > '() > (cons (rvrs (cdr lista)) (list (car lista))))) > > CL-USER> (rvrs '(1 2 3)) > (((NIL 3) 2) 1) Maybe you should learn a bit more about the basic list operations. Some intersting examples for your problem: (cons 1 (cons 2 (list 3))) (list 2 3) (append (list 1) (cons 2 nil)) Please try to say the results without testing it in the REPL. If you fully understand how lists work in Lisp, it should be no problem for you to fix the rvrs function. -- Frank Buss, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.