
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)

On Sat, Dec 24, 2011 at 11:35 AM, Andy
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.
You may produce this list with iterate and takeWhile, or use until then reverse. You'll need filter and map to finish the algorithm. Good luck ! -- Jedaï

On Sat, Dec 24, 2011 at 11:35 AM, Andy
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.
Why IO errors ? The algorithms is clearly pure, you should write it as an independent function, probably : main1 = do putStrLn "Russian Peasant Multiplication \n" putStrLn "Enter 2 integers : " a <- getInt b <- getInt print (russianMul a b) Or if you want to have the intermediate list : main1 = do putStrLn "Russian Peasant Multiplication \n" putStrLn "Enter 2 integers : " a <- getInt b <- getInt let rows = firstStep a b result = secondStep rows print rows print result russianMul = secondStep . firstStep firstStep a b = .... -- Jedaï
participants (2)
-
Andy
-
Chaddaï Fouché