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

  • Home
  • SEARCH
  • 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 8659163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:56:39+00:00 2026-06-12T15:56:39+00:00

The code below solves hanoi returning a list of moves using predefined functions moveLOD,swapLOI

  • 0

The code below solves hanoi returning a list of moves using predefined functions moveLOD,swapLOI and swapLID.

MoveLOD: moves 1 disc from the first position to third the pin in the third position of the triplet. Additionally a string with information about the movement is piling on list of strings.

type Pin = (Char, Int)        -- Represents a rod, named for a character and the number of disks in it.
type Plate = (Pin, Pin, Pin)  -- Represents the configuration of the three rods.(Origin,Intermediate,Destination
type Log = (Plate, [String])  -- Represents a state formed by the configuration of rods and a list of strings that will record the movements made by the algorithm.


moveLOD :: Log -> Log
moveLOD (((o,n), i ,(d,k)),s) = (((o,n-1), i ,(d,k+1)), (o:" -> " ++ [d]):s)

-- swapLOI: Change the positions of the origin rods and intermediate rods.
swapLOI:: Log->Log
swapLOI ((o,i,d),s) = ((i,o,d),s) 

-- swapoLID : Change the positions of the intermediate rods and destination rods.
swapLID:: Log->Log
swapLID ((o,i,d),s) = ((o,d,i),s)

hanoi :: Log -> Log
hanoi:: Int->Log->[String]
hanoi 1 log = transformaLista(moveLOD log)
hanoi n log = hanoi (n-1) (swapLID log) ++ hanoi 1 log ++ hanoi (n-1) (swapLOI(log))

changeToList::Log->[String]
changeToList(p,s) = s

callHanoi:: Int->[String]
callHanoi n = hanoi n ((('O',n),('I',0),('D',0)),[])
  • 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-06-12T15:56:41+00:00Added an answer on June 12, 2026 at 3:56 pm
    hanoi :: Log -> Log
    hanoi ((('o',0),i,d),s) = ((('o',0),('i',0),('d',0)), [])
    hanoi ((('o',1),i,d),s) = moveLOD((('o',1),i,d),s)
    hanoi ((('o',n),i,d),s)= hanoi(swapLOI(hanoi(swapLOI(swapLID(moveLOD(swapLID((('o',n),i,d),s)))))))
    

    only defines the function for arguments where the Char in the first Pin of the Plate is 'o', you also need to provide equations for when the character is something else.

    When an argument not matching any of the patterns for which there is a defining equation is received, a “Non-exhaustive pattern” error is raised. The only way to fix it is to provide equations for the remaining patterns.

    In your revised code, first, your treatment of the case where the origin pin is empty is incorrect,

    hanoi (((o,0),i,d),s) = ((('o',0),('i',0),('d',0)),[])
    

    means that whenever this case applies the result is the same, regardless of what d and i are. When hanoi is called from chamahanoi with an argument greater than 2, at some time the origin pole becomes empty, and since above that in the call chain are only hanoi and swapLOI, that constant result bubbles up. You get the correct result for n == 2 (n == 1 is directly solved by the second equation) since the recursive calls to hanoi then both have only one disk on the origin pole.

    That case should be

    hanoi (((o,0),i,d),s) = (((o,0),i,d),s)
    

    That still doesn’t produce correct results (wrong sequence of moves), since the recursion in the general case is wrong.

    You

    • move the top disk to the intermediate pin (swapLID . moveLOD . swapLID);
    • then move the remaining disks to the destination (hanoi), but that isn’t allowed since the smallest disk is on the intermediate pin and so no other disk may be placed there;
    • finally, move the disk(s) from the intermediate pin to the destination using the (now empty) origin pin as intermediate.

    You should

    • move n-1 disks from the origin to the intermediate pin,
    • then move the bottom (largest) disk to the destination,
    • finally, move the n-1 disks from the intermediate to the destination.

    I don’t see an easy way to do that without an extra argument keeping track of how many disks to move. Consider a four-disk game. First, the top three disks are moved to the intermediate pin, then the bottom disk is moved to the destination pin. Now the task is to move the three disks from the intermediate pin to the destination pin, using the origin pin as helper.

    The correct way is the sequence

    1. i -> d (([],[1,2,3],[4]) -> ([],[2,3],[1,4]))
    2. i -> o (([],[2,3],[1,4]) -> ([2],[3],[1,4]))
    3. d -> o (([2],[3],[1,4]) -> ([1,2],[3],[4]))
    4. i -> d (([1,2],[3],[4]) -> ([1,2],[],[3,4]))
    5. o -> i (([1,2],[],[3,4]) -> ([2],[1],[3,4]))
    6. o -> d (([2],[1],[3,4]) -> ([],[1],[2,3,4]))
    7. i -> d (([],[1],[2,3,4]) -> ([],[],[1,2,3,4]))

    After step 2, the original destination pin becomes the pin from which disks (well, one) are to be moved to o, but the lowest of those shall not be moved in this situation. How could that be achieved if the only information is how many disks are on each pin, and from where to where disks shall be moved?

    If you change the type of hanoi to

    hanoi :: Int -> Log -> Log
    

    and call it

    chamahanoi n = hanoi n ((('o',n),('i',0),('d',0)),[])
    

    it is easy to implement.

    If you don’t want to do that, or are not allowed to, you could either keep track of the sizes on each pin, and only move disks onto larger ones, or you could sneakily remove and add disks at the appropriate pins to emulate that restriction, but that would be hard to distinguish from cheating without proper explanation.

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

Sidebar

Related Questions

Code below is used to save PostgreSql database backup from browser in Apache Mono
The code below is a simplified version of the pattern my project is using.
I am trying to create a CSV file using the C# code below: string
The code below is from a book,so it'll not be incorrect.But I don't know
The code below works perfect for binding actual urls grabbed from the net. My
Using the code below I am displaying pop up windows. I used the toggle()
I m using HttpWebRequest class asynchronously as code below (its just windows application) private
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards
Code below is working well as long as I have class ClassSameAssembly in same
The code below simply didn't work. document.getElementById('files').addEventListener('change', handleFileSelect, false); reported by firebug that this

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.