LispForum

My first program in Lisp.

My first program in Lisp.

Posted: Mon Nov 07, 2011 9:31 am

by bokshi

Hi all. I am new to this forum and new to the world of lisp. I am doing my first program in lisp but having trouble to increment the count. I have posted my code. below. When I try to find the first position it gives me the correct one but when I try to get the second or third it give me 1 all the time. Please help me out guys.

;; it counts the position number in the list.;; count function( defun count ()( + 0 1))

Re: My first program in Lisp.

Posted: Mon Nov 07, 2011 7:58 pm

by krzysz00

bokshi wrote:Hi all. I am new to this forum and new to the world of lisp. I am doing my first program in lisp but having trouble to increment the count. I have posted my code. below. When I try to find the first position it gives me the correct one but when I try to get the second or third it give me 1 all the time. Please help me out guys.

Hi, welcome to LispForum. I'm glad you're trying to learn more about Common Lisp. However, there are a few problems with the code.

First, the formatting. You should be using an editor that is aware of the syntax of Common Lisp, such as Emacs. Also, closing parenthesis usually don't get their own line, and the bodies of forms are indented by spaces. In addition, you should post your code between

;; it counts the position number in the list.;; count function(defun count () (+ 0 1))

Then, on to the matter of your program. The count function, which is never called, always returns 1. The `count' variable in `position-in-list' is never defined, and you call your function with three arguments in the `t' branch of the `cond'. However, you have the basic structure of a position function. Here is a version that will actually produce the desired result:

It is possible to avoid the use of two functions by using the `labels' special form and closures, which might be concepts you haven't gotten to yet. If you don't understand this version, please ask and I will try to go through it.