1

I am trying to execute few statements in my function in Haskell, I looked online and got the idea that may be if I use "do" I can be able to do that.I used that but it still is not working for me, If some one can please have a look and guide me what I am doing wrong, I have just started with haskell, so its a little struggle with Haskell syntax.

My function:

type Rod = String
Move = (Integer, Rod, Rod)
hanoi :: Integer -> Rod -> Rod -> Rod -> [Move]
hanoi n source helper destination= if n==1 then [(n source destination)] else do
                                (hanoi (n-1) source helper destination) 
                                ([n source destination]) 
                                (hanoi (n-1) helper destination source)

I am trying to do the towers of Hanoi problem.and I want to execute the three statements that are after "do". Any help would be highly appreciated .

Thanks in advance!!!

3
  • 4
    Haskell is an expression-oriented language. Everything on the RHS of an = (or -> in case expressions) is an expression.do blocks are fancy expressions, but not appropriate for this use case. For your case, you really should think about what the result needs to be. Then write an expression describing it. Commented Jan 13, 2016 at 14:37
  • 3
    I think you are pretty close - you just have to realize that you don't want to do something - indeed you are trying to return a list of moves - you have to concat the results somehow ;) .... also to the downvoters: shame on you - this is a good question - maybe not PhD level but please keep the community friendly to beginners! Commented Jan 13, 2016 at 14:56
  • 1
    @Carsten Thank you for such sweet, kind and motivational comment, I will try to think of concating the result.Its good to hear that I am some what close to the solution :) Thanks again!!!! Commented Jan 13, 2016 at 15:03

1 Answer 1

9

to help you out a bit here is a way to get it to compile and work (well almost):

type Rod = String
type Move = (Integer, Rod, Rod)

hanoi :: Integer -> Rod -> Rod -> Rod -> [Move]
hanoi n source helper destination =
  if n==1 then
    [(n, source, destination)]
  else
    hanoi (n-1) source helper destination 
    ++ [(n, source, destination)]
    ++ hanoi (n-1) helper destination source

the things I changed are:

  • gave a type for Move (I hope you wanted a tuple)
  • changed the results into tuple ((n source destination) -> (n,source,destination))
  • concatenated the results with ++

Now you only have to fix a slight problem with the order of the operations ;) and it should print you a solution :D

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you a millions, indeed this was what I was struggling with. Thank you for being so kind and helpful!!!!! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.