My first program in Lisp.

Discussion of Common Lisp

My first program in Lisp.

Postby bokshi » Mon Nov 07, 2011 9:31 am

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)
)
bokshi
 
Posts: 2
Joined: Mon Nov 07, 2011 8:35 am

Re: My first program in Lisp.

Postby krzysz00 » Mon Nov 07, 2011 7:58 pm

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
Code: Select all
tags. With these hints in mind, here is a re-formatted version of your code:
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))

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:
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))

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.
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)))

Good luck with your Common Lisp endeavors!

KAD
krzysz00
 
Posts: 4
Joined: Mon Nov 07, 2011 5:10 pm


Return to Common Lisp

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests