Nia
Nia

Reputation: 151

Executing multiple commands in the same function (Haskell)

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!!!

Upvotes: 1

Views: 1169

Answers (1)

Random Dev
Random Dev

Reputation: 52270

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

Upvotes: 9

Related Questions