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.
;; comment
;; question
;; (position-in-list 'a '(a b c d))
;; -> 1
;; Program
(defun position-in-list (letter list) )
( cond
( (null list) nill
)
( (eq (car list) letter) count
)
( t (position-in-list letter (cdr list)) count)
)
)
;; it counts the position number in the list.
;; count function
( defun count ()
( + 0 1)
)
My first program in Lisp.
Re: My first program in Lisp.
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.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.
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
Code: Select all
Code: Select all
;; comment
;; question
;; (position-in-list 'a '(a b c d))
;; -> 1
;; Program
(defun position-in-list (letter list)
(cond
((null list) nil)
((eq (car list) letter) count)
(t (position-in-list letter (cdr list)) count)))
;; it counts the position number in the list.
;; count function
(defun count ()
(+ 0 1))
Code: Select all
;; comment
;; question
;; (position-in-list 'a '(a b c d))
;; -> 0
;; Program
(defun %position-in-list (letter list count)
(cond
((null list) nil)
((eq (car list) letter) count)
(t (%position-in-list letter (cdr list) (1+ count)))))
(defun position-in-list (letter list)
(%position-in-list letter list 0))
Code: Select all
;; comment
;; question
;; (position-in-list 'a '(a b c d))
;; -> 0
;; Program
(defun position-in-list (letter list)
(labels
((%position-in-list (list count)
(cond
((null list) nil)
((eq (car list) letter) count)
(t (%position-in-list (cdr list) (1+ count))))))
(%position-in-list list 0)))
KAD