Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:08:03+00:00 2026-05-26T05:08:03+00:00

My code in lisp is as follows: (defun solve-hanoi(from) (hanoi (length from) from ‘()

  • 0

My code in lisp is as follows:

(defun solve-hanoi(from) (hanoi (length from) from '() '()))    

(defun hanoi(height from to aux) (when (>= height 1) 
                   (hanoi (- height 1) from aux to)
                   (format t "~%Move ~a from ~a to ~a" (nth 0 from) from to)
                   (push (pop from) to) 
                   (hanoi (- height 1) aux to from)))

I am new to lisp and have NO clue as to what I am doing wrong.
Help with this would be GREATLY appreciated since I’ve been at this for hours.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:08:03+00:00Added an answer on May 26, 2026 at 5:08 am

    The recursive algorithm is:

    To move n discs from peg A to peg C:
    1. move n−1 discs from A to B. This leaves disc n alone on peg A
    2. move disc n from A to C
    3. move n−1 discs from B to C so they sit on disc n

    (From http://en.wikipedia.org/wiki/Tower_of_Hanoi#Recursive_solution)

    Since the meaning of A, B and C (your from, aux and to) are relative to the current iteration and keep changing, it’s easier not to pass the state of the game around and trying to understand what it means, but to simply generate solving instructions.

    To implement the algorithm above in this way, you need the following inside your (when (>= height 1):
    1. Recursive call with n-1, swapping B and C. You got this one right already.
    2. Print info about the move, for instance (format t "~%Move ~a to ~a" from to).
    3. Recursive call with n-1, swapping A and B. You got this one right too.

    Then change your (solve-hanoi) so that it takes the number of disks on the first rod as argument, and calls (hanoi) with this number and whatever names you want for the rods, for instance (hanoi 4 'A 'B 'C) or (hanoi 4 1 2 3) for 4 disks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Common Lisp code: (defun micro-read-eval-print () (format t Micro >
I have the following LISP code (defun l (x y) (list x y)) when
I'm porting some code from lisp, but I got stuck at this part (apparently
I am translating some code from lisp to Python. In lisp, you can have
I have the following code in Lisp: (defun Is_List_Even (lista) (cond ((null lista) t)
Is there a way in Lisp to include code from other Lisp files? For
When writing Common Lisp code, I use SLIME. In particular, I compile the buffer
I have some lisp initialisation code: (eval-when (:compile-toplevel :load-toplevel :execute) (require 'asdf)) (eval-when (:compile-toplevel
I've got this snippet of code: (define-key lisp-interaction-mode-map (kbd C-c C-e) (lambda () (let
I am trying to write some of my first code in emacs lisp and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.