Mike Meyer schreef op 19-2-2015 om 8:25:
On Thu, Feb 19, 2015 at 1:11 AM, Roelof Wobben <r.wobben@home.nl> wrote:
I think I found the answer by some trail and error,

hanoi 1 start _ end = [ (start, end)]
hanoi n start temp end = hanoi (n-1) start end temp ++ [(start, end)] ++ hanoi (n-1) temp start end

main = print $ hanoi 3 'a' 'b' 'c'

I mentioned earlier that using "hanoi 1 start temp end" instead of "[(start, end)]" made things read a bit better to me. I figured out why. It isolates the final representation of a "move" to the base case only. So if you make that change, you could then write your base case as:

hanoi 1 start _ end = "move 1 disk from " ++ start ++ " to " ++ end ++ ".\n"  

and the other case still works correctly if you used the "hanoi 1" version. You'll want to change "print" to "putStr" in main, but it will now print the list of moves in English instead of Haskell.


_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

Thanks,

1 remark.

You have to do this hanoi 1 start _ end = "move 1 disk from " ++ [start] ++ " to " ++ [end] ++ ".\n" 
because in your code you get this error message :

*Main> main                                                                                                                                                                                                          
[('a','c'),('a','b'),('c','b'),('a','c'),('b','a'),('b','c'),('a','c')]                                                                                                                                              
*Main> :l hanoi                                                                                                                                                                                                      
[1 of 1] Compiling Main             ( hanoi.hs, interpreted )                                                                                                                                                        
                                                                                                                                                                                                                     
hanoi.hs:4:24:                                                                                                                                                                                                       
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’                                                                                                                                                    
    In the second argument of ‘hanoi’, namely ‘'a'’                                                                                                                                                                  
    In the second argument of ‘($)’, namely ‘hanoi 3 'a' 'b' 'c'’                                                                                                                                                    
    In the expression: print $ hanoi 3 'a' 'b' 'c'                                                                                                                                                                   
                                                                                                                                                                                                                     
hanoi.hs:4:28:                                                                                                                                                                                                       
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’                                                                                                                                                    
    In the third argument of ‘hanoi’, namely ‘'b'’                                                                                                                                                                   
    In the second argument of ‘($)’, namely ‘hanoi 3 'a' 'b' 'c'’                                                                                                                                                    
    In the expression: print $ hanoi 3 'a' 'b' 'c'                                                                                                                                                                   
                                                                                                                                                                                                                     
hanoi.hs:4:32:                                                                                                                                                                                                       
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’                                                                                                                                                    
    In the fourth argument of ‘hanoi’, namely ‘'c'’                                                                                                                                                                  
    In the second argument of ‘($)’, namely ‘hanoi 3 'a' 'b' 'c'’                                                                                                                                                    
    In the expression: print $ hanoi 3 'a' 'b' 'c'                                                                                                                                                                   
Failed, modules loaded: none.                                                                                                                                                                                        
Prelude> :l hanoi                                                                                                                                                                                                    
[1 of 1] Compiling Main             ( hanoi.hs, interpreted )                                                                                                                                                        
                                                                                                                                                                                                                     
hanoi.hs:4:25:                                                                                                                                                                                                       
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’                                                                                                                                                    
    In the second argument of ‘hanoi’, namely ‘'a'’                                                                                                                                                                  
    In the second argument of ‘($)’, namely ‘hanoi 3 'a' 'b' 'c'’                                                                                                                                                    
    In the expression: putStr $ hanoi 3 'a' 'b' 'c'                                                                                                                                                                  
                                                                                                                                                                                                                     
hanoi.hs:4:29:                                                                                                                                                                                                       
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’                                                                                                                                                    
    In the third argument of ‘hanoi’, namely ‘'b'’                                                                                                                                                                   
    In the second argument of ‘($)’, namely ‘hanoi 3 'a' 'b' 'c'’                                                                                                                                                    
    In the expression: putStr $ hanoi 3 'a' 'b' 'c'                                                                                                                                                                  
                                                                                                                                                                                                                     
hanoi.hs:4:33:                                                                                                                                                                                                       
    Couldn't match expected type ‘[Char]’ with actual type ‘Char’                                                                                                                                                    
    In the fourth argument of ‘hanoi’, namely ‘'c'’                                                                                                                                                                  
    In the second argument of ‘($)’, namely ‘hanoi 3 'a' 'b' 'c'’                                                                                                                                                    
    In the expression: putStr $ hanoi 3 'a' 'b' 'c'               

Now I  have to figure out  how this works before I can work on the optional exercise hanoi with  4 pegs.

Roelof