Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  construct list (Chadda? Fouch?)
   2. Re:  construct list (Chadda? Fouch?)
   3.  Prelude Implementation of Last overlapping       patterns?
      (Mark Stoehr)
   4. Re:  Prelude Implementation of Last overlapping   patterns?
      (Benjamin Edwards)
   5. Re:  Prelude Implementation of Last overlapping   patterns?
      (Tom Murphy)


----------------------------------------------------------------------

Message: 1
Date: Sat, 24 Dec 2011 12:04:53 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] construct list
To: Andy <[email protected]>
Cc: [email protected]
Message-ID:
        <CANfjZRaFocfSf15zB4kjRG6hTf9rs4ONpbpWZ=jfmvrzdas...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Dec 24, 2011 at 11:35 AM, Andy <[email protected]> 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?



------------------------------

Message: 2
Date: Sat, 24 Dec 2011 12:14:34 +0100
From: Chadda? Fouch? <[email protected]>
Subject: Re: [Haskell-beginners] construct list
To: Andy <[email protected]>
Cc: [email protected]
Message-ID:
        <canfjzrz3_6e68xvgb9nsjvyom+v3nsz5urtohdkdxj7a67n...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Sat, Dec 24, 2011 at 11:35 AM, Andy <[email protected]> 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.

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?



------------------------------

Message: 3
Date: Sat, 24 Dec 2011 11:48:25 -0600
From: Mark Stoehr <[email protected]>
Subject: [Haskell-beginners] Prelude Implementation of Last
        overlapping     patterns?
To: [email protected]
Message-ID:
        <CAPLV0cghXP+pRaPWW1+iGjTkek0=ugei3jno0xty+sqppoj...@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1

The standard Prelude implementation of 'last' is as follows
\begin{code}
last             :: [a] -> a
last [x]         =  x
last (_:xs)      =  last xs
last []          =  error "Prelude.last: empty list"
\end{code}

Isn't [x] equivalent to (x:[]) hence wouldn't [1] match both [x] and
(_:xs)?  If that's the case then we would have
last [1] == 1
and
last [1] == last []

but that doesn't happen. Why?



------------------------------

Message: 4
Date: Sat, 24 Dec 2011 17:53:14 +0000
From: Benjamin Edwards <[email protected]>
Subject: Re: [Haskell-beginners] Prelude Implementation of Last
        overlapping     patterns?
To: Mark Stoehr <[email protected]>
Cc: [email protected]
Message-ID:
        <CAN6k4nj2FXACg5=1ak7twxpau_0eue-00xrkt_ydng-n+g_...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Because patterns are matched in order and the first one match is taken.
On 24 Dec 2011 17:49, "Mark Stoehr" <[email protected]> wrote:

> The standard Prelude implementation of 'last' is as follows
> \begin{code}
> last             :: [a] -> a
> last [x]         =  x
> last (_:xs)      =  last xs
> last []          =  error "Prelude.last: empty list"
> \end{code}
>
> Isn't [x] equivalent to (x:[]) hence wouldn't [1] match both [x] and
> (_:xs)?  If that's the case then we would have
> last [1] == 1
> and
> last [1] == last []
>
> but that doesn't happen. Why?
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111224/aeaf3f39/attachment-0001.htm>

------------------------------

Message: 5
Date: Sat, 24 Dec 2011 20:16:14 -0500
From: Tom Murphy <[email protected]>
Subject: Re: [Haskell-beginners] Prelude Implementation of Last
        overlapping     patterns?
To: Benjamin Edwards <[email protected]>
Cc: Mark Stoehr <[email protected]>, [email protected]
Message-ID:
        <CAO9Q0tV7z2_CvrsQ-WEQVRx_Y1ihX+3zAEPbw8aL-=i140g...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

On Dec 24, 2011 12:53 PM, "Benjamin Edwards" <[email protected]> wrote:
>
> Because patterns are matched in order and the first one match is taken.
>

This is very expressive. For example, you can write
f 0 = 0
f x = 1 / x

> On 24 Dec 2011 17:49, "Mark Stoehr" <[email protected]> wrote:
>>
>> The standard Prelude implementation of 'last' is as follows
>> \begin{code}
>> last             :: [a] -> a
>> last [x]         =  x
>> last (_:xs)      =  last xs
>> last []          =  error "Prelude.last: empty list"
>> \end{code}
>>
>> Isn't [x] equivalent to (x:[]) hence wouldn't [1] match both [x] and
>> (_:xs)?  If that's the case then we would have
>> last [1] == 1
>> and
>> last [1] == last []
>>
>> but that doesn't happen. Why?
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111224/3a06dd8e/attachment-0001.htm>

------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 42, Issue 27
*****************************************

Reply via email to