[Haskell-beginners] construct list

Andy ablaroc at gmail.com
Sat Dec 24 11:35:13 CET 2011


I have been trying to simulate "Russian peasant multiplication" in 
Haskell , but I can't produce a list of tuples after I enter 2 integers. 
I have tried constructing many accumulator functions to do so, but keep 
getting IO errors. This is NOT a homework question. I am a senior 
learning Haskell, and finding it very enjoyable, although frustrating at 
times.
----------------------------------------------------------------------------------
main1 = do
         putStrLn "Russian Peasant Multiplication \n"
         putStrLn "Enter 2 integers : "
         a <- getInt
         b <- getInt
         -- at this point I would like a list of tuples of the form [ (a 
`div` 2 , b*2 )] , starting with the 2 initial integers, down to
         -- where the first value is 1 in the last tuple: ie if the 
initial numbers were 121 and 3 say , I'd like the list
         -- [ (121,3) , (60,6) , (30,12) , (15,24) , (7,48) , (3,96) , 
(1,192) ] produced and available in main for further manipulation.

----------------------------------------
-- to read in an Int , I used :
getInt :: IO Int
getInt = do line <- getLine
             return( read line :: Int)



More information about the Beginners mailing list