Hello! I'm currently working on a lisp program that should increment a number stored in a list through its digits by 1. I've written a module that recieves the reversed list and a carry flag but it won't even compile, so I can't tell if I'm on the right path or not. Can you please look at the code and tell me where I'm screwing up? I'm a complete noob so it's probably just a syntax error. Thank you very much in advance.

Mapcar will call a provided function on each element of a list, and return a new list containing the result of that function for each item in the original list. You can look it up in the Hyperspec for a more complete description.

If you wanted a recursive version, it is here. Don't peek if you wanted to do it yourself. Keep in mind that you don't have to reimplement MOD, and you especially don't need to do it inline—even if you did have to write it yourself, you should have put it in another function (named MOD, perhaps). Also keep in mind that addition does not modify anything. It just returns a value. This recursive function is not tail recursive, so it is sure to fail on long lists. A tail recursive version follows. If tail calls are optimized in your Lisp, this one will work on arbitrarily long lists.

Wow, you guys are great! It worked, but only after I made all modifications you specified. I really didn't expect answers to come in this soon (i didn't expect one at all actually).@Exolon: Thanx man, you saved my life, I would've never have spotted such a dumb mistake. Now that I got that to work I managed to check its result and do the necessary modifications in order to have it do what it should.@qbg: I'm using uppercase since that's what the examples I've read use.I know it's not really necessary but it adds a bit of readability, since I don't have a good IDE for writing code in LISP. The parantheses are aligned like that to add some more ease in reading the code by trying to order it in blocks. The program should take a list with a number stored by its digits and increase the number by one. The function that does this has to be recursive.@Paul Donnelly: Thank you for drawing my attention to that (using (1) instead of '(1) ), I don't use ifs because the program is for a course of logical and functional programming and it would be awkward if I started using new keywords to complete my laboratories. I want to keep things simple and straight to the point when I do the laboratory tasks. But i'll research ifs as you suggested since they'll surely help me with the written or practical exams.That being said here's the final code, I couldn't have done this without your help.

haplo wrote:@qbg: I'm using uppercase since that's what the examples I've read use.I know it's not really necessary but it adds a bit of readability, since I don't have a good IDE for writing code in LISP. The parantheses are aligned like that to add some more ease in reading the code by trying to order it in blocks. The program should take a list with a number stored by its digits and increase the number by one. The function that does this has to be recursive.

Emacs will help with formatting code the standard way. (There exists a windows version if you are using that) LispWorks Personal Edition's built in editor is good and it is a free download (though not free software). For reading code in blocks, the standard style shows blocks by indentation.

Now then, are you wanting incr to do this:(incr '(7 8 9)) => (7 9 0)?

Oh, and if you have any suggestions concerning solving this problem in a more orthodox way please tell me your opinions as I have first solved it in c recursively and translated the code in lisp.

If my interpretation above is correct, here is a somewhat non-orthodox way to do it: