Help understanding my error
Posted: Wed Nov 07, 2012 8:09 pm
I posted this on Common Lisp area because that's what I'm learning, but only after submitting it I realized this is the best place to ask questions about "learning"/homework. Sorry for the duplicate post, it won't happen again. If the moderators want to discard my first post on CL area, ok.
Hi. I'm completely new to Common Lisp and trying to learn something about functional programming . I'm using SBCL on OS X with Emacs/Slime. I'm trying to do the exercises of Paul Graham's book, ANSI Common Lisp, and there's one that it seams to be ok to me but it doesn't give the right answer. Exercise 3-5 asks for a recursive function that takes a list and returns another list of each element plus its position. So,
My code is bellow, and when I call the function with the same list above the answer is only (7). Can anyone tell me what I'm doing wrong?
Hi. I'm completely new to Common Lisp and trying to learn something about functional programming . I'm using SBCL on OS X with Emacs/Slime. I'm trying to do the exercises of Paul Graham's book, ANSI Common Lisp, and there's one that it seams to be ok to me but it doesn't give the right answer. Exercise 3-5 asks for a recursive function that takes a list and returns another list of each element plus its position. So,
Code: Select all
> (pos+ '(7 5 1 4))
(7 6 3 7)
Code: Select all
(defun pos+ (lst)
(pos-rec lst 0 ()))
(defun pos-rec (lst i result)
(if lst
(progn (push (+ (car lst) i) result)
(pos-rec (cdr lst) (+ i 1) result)))
result)