[Haskell-beginners] construct list

Chaddaï Fouché chaddai.fouche at gmail.com
Sat Dec 24 12:04:53 CET 2011


On Sat, Dec 24, 2011 at 11:35 AM, Andy <ablaroc at gmail.com> wrote:
> 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ï



More information about the Beginners mailing list