3 Replies - 895 Views - Last Post: 25 May 2011 - 06:29 PM

#1 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Common Lisp: Recursive Call

Posted 25 May 2011 - 07:07 AM

Hello, I'm very new to Common Lisp and am trying to write a simple program that takes two numbers until you enter a number that is not one of the two.

defun calc ()
	(princ "Please enter a grade or done: ")
	(let ((g (read)))
		(cond ((equal g 3) (calc)
				(equal g 4) (calc))
				(t '(nil))))) 


It works as expected when I first enter a 3. However, if I enter a 4 to begin with it goes directly to the 'nil, and does not make the recursive call back to calc. Also, if I start with 3, enter some more 3's and 4's, it sometimes goes to the 'nil when I enter a 4. Here's some example output of what I am seeing:

[88]> (calc)
Please enter a grade or done: 4

(NIL)

[89]> (calc)
Please enter a grade or done: 3
Please enter a grade or done: 3
Please enter a grade or done: 4
Please enter a grade or done: 4
Please enter a grade or done: 3
Please enter a grade or done: 4
Please enter a grade or done: 4

(NIL)

Any tips? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Common Lisp: Recursive Call

#2 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11022
  • View blog
  • Posts: 18,805
  • Joined: 19-March 11

Re: Common Lisp: Recursive Call

Posted 25 May 2011 - 09:38 AM

I don't have the capacity to test this from where I'm sitting right now, but I think I see a paren balancing problem after the first condition of your cond. I think it should be
(cond ((equal g 3) (calc))


This may fix it, but as I say I don't have lisp on this machine.

This post has been edited by jon.kiparsky: 25 May 2011 - 09:39 AM

Was This Post Helpful? 3
  • +
  • -

#3 I X Code X 1  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 47
  • View blog
  • Posts: 66
  • Joined: 28-July 10

Re: Common Lisp: Recursive Call

Posted 25 May 2011 - 11:29 AM

Thanks for the help. I had two instances of paren problems. This is the correct code for what I was trying to do:

 defun calc ()
	(princ "Please enter a grade or done: ")
	(let ((g (read)))
		(cond ((equal g 3) (calc))
				((equal g 4) (calc))
				(t '(nil))))) 



Thanks again!
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11022
  • View blog
  • Posts: 18,805
  • Joined: 19-March 11

Re: Common Lisp: Recursive Call

Posted 25 May 2011 - 06:29 PM

Glad I could help!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1