Re: doubling list elements at every level using recursion
- From: Slobodan Blazeski <slobodan.blazeski@xxxxxxxxx>
- Date: Tue, 14 Apr 2009 11:58:31 -0700 (PDT)
On Apr 14, 8:53 pm, Slobodan Blazeski <slobodan.blaze...@xxxxxxxxx>
wrote:
On Apr 14, 8:43 am, pereges <Brol...@xxxxxxxxx> wrote:
thanks for all suggestions!
can someone also help me with a similar problem:
4. Define an iterative function idouble that takes a list. The
function idouble doubles every number at the top level in its argument
and does not alter other members of the argument.
Tests of idouble:
(idouble nil) ; NIL
(idouble '(1 2 3)) ; (2 4 6)
(idouble '(1 (2) 3)) ; (2 (2) 6)
(idouble '(1 4 a e b 6)) ; (2 8 A E B 12)
In this problem I cannot figure out how to iterate through a list
while at the same time modify its contents. This is the code I tried:
(defun idouble (lst)
(if (null lst)
NIL
(dolist (x lst)
(if (number p x)
(cons (* 2 x) (cdr lst))
(cons x (cdr lst)))))
I hope this isn't a homework
(defun idouble (x)
(mapcar #'(lambda (n) (if (numberp n) (* 2 n) n)) x))
CL-USER 2 > (idouble nil)
NIL
CL-USER 3 > (idouble '(1 2 3))
(2 4 6)
CL-USER 4 > (idouble '(1 (2) 3))
(2 (2) 6)
CL-USER 5 > (idouble '(1 4 a e b 6))
(2 8 A E B 12)
bobihttp://tourdelisp.blogspot.com/
Wait the minute what do you mean by iterative solution?
(defun idouble (x)
(let (res)
(dolist (e x (nreverse res))
(push (if (numberp e) (* 2 e) e) res))))
bobi
.
- References:
- doubling list elements at every level using recursion
- From: pereges
- Re: doubling list elements at every level using recursion
- From: Pascal J. Bourguignon
- Re: doubling list elements at every level using recursion
- From: pereges
- Re: doubling list elements at every level using recursion
- From: Slobodan Blazeski
- doubling list elements at every level using recursion
- Prev by Date: Re: doubling list elements at every level using recursion
- Next by Date: Re: doubling list elements at every level using recursion
- Previous by thread: Re: doubling list elements at every level using recursion
- Next by thread: Re: doubling list elements at every level using recursion
- Index(es):