[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell
Subject:    Re: [Haskell] Re: Help : A problem with IO
From:       "abdullah abdul Khadir" <abdullah.ak2002 () gmail ! com>
Date:       2008-11-27 18:22:57
Message-ID: 719881900811271010r313d3becqefa9fa4b772b5666 () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thanks all,
                I got it working finally. What did i learn ?
a) I need to put a do after else for more than one instruction (?)
b) All similar type of questions are to be redirected to haskell-beginner
and haskell-cafe

Points noted.
Thank you once again,
Abdullah Abdul Khadir



On Wed, Nov 26, 2008 at 10:56 PM, wman <666wman@gmail.com> wrote:

> if it should really read whole line, it must try to somehow chain the
> values into the string / list of chars (using (:) )
>
> getMyLine :: IO [Char]
> getMyLine =  do
>   c <- getChar
>   if (c == '\n')
>     then return "" -- imho [] looks nicer ;-)
>     else do
>       rest <- getMyLine
>       return $ c : rest
>
> -- or
>
> getMyLine :: IO [Char]
> getMyLine =  do
>   c <- getChar
>   if (c == '\n')
>     then return "" -- imho [] looks nicer ;-)
>     else getMyLine >>= return . (c:)
>
> -- or even shorter and still equivalent ;-)
>
> getMyLine :: IO [Char]
> getMyLine =  getChar >>= (\c -> if (c == '\n') then return [] else fmap
> (c:) getMyLine)
>
>
>
> 2008/11/26 abdullah abdul Khadir <abdullah.ak2002@gmail.com>
>
>> Hi,
>>
>>     The function getMyLine written by me is intended for getting a
>> complete string from the standard input.
>>
>> import IO
>>
>> getMyLine :: IO [Char]
>> getMyLine =  do
>>                 c <- getChar
>>                 if(c == '\n')
>>                         then return ""
>>                         else    cs <- getMyLine
>>                                 return [c]
>>
>> However I keep getting the following error:
>>
>> io.hs:14:2: Parse error in pattern
>> Failed, modules loaded: none.
>>
>>         I fail to understand what the error is. I tried out various things
>> such as changing the alignment and so on to no avail. The following program,
>> however, compiled successfully though it has the same structure as the
>> previous program.
>>
>> checkd :: IO Bool
>> checkd = do
>>                 c <- getChar
>>                 if(c=='d')
>>                         then    return True
>>                         else    return False
>>
>> Prelude> :load ./io.hs
>> [1 of 1] Compiling Main             ( io.hs, interpreted )
>> Ok, modules loaded: Main.
>> *Main> checkd
>> d
>> True
>>
>>
>> _______________________________________________
>> Haskell mailing list
>> Haskell@haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell
>>
>>
>

[Attachment #5 (text/html)]

Thanks all,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
I got it working finally. What did i learn ?<br>a) I need to put a do after else for \
more than one instruction (?)<br>b) All similar type of questions are to be \
redirected to haskell-beginner and haskell-cafe<br> \
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>Points noted. <br>Thank \
you once again,<br>Abdullah Abdul Khadir<br><br><br><br><div class="gmail_quote">On \
Wed, Nov 26, 2008 at 10:56 PM, wman <span dir="ltr">&lt;<a \
href="mailto:666wman@gmail.com">666wman@gmail.com</a>&gt;</span> wrote:<br> \
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); \
margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">if it should really read whole line, \
it must try to somehow chain the values into the string / list of chars (using (:) \
)<div class="Ih2E3d"> <br><br>getMyLine :: IO [Char]<br>
getMyLine =&nbsp; do<br>
&nbsp; c &lt;- getChar<br>
&nbsp; if (c == &#39;\n&#39;)<br></div>
&nbsp;&nbsp;&nbsp; then return &quot;&quot; -- imho [] looks nicer ;-)<br>
&nbsp;&nbsp;&nbsp; else do<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; rest &lt;- \
getMyLine<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $ c : rest<br> <br>-- or<div \
class="Ih2E3d"><br><br>getMyLine :: IO [Char]<br>getMyLine =&nbsp; do<br>&nbsp; c \
&lt;- getChar<br>&nbsp; if (c == &#39;\n&#39;)<br></div>&nbsp;&nbsp;&nbsp; then \
return &quot;&quot; -- imho [] looks nicer ;-)<br>&nbsp;&nbsp;&nbsp; else getMyLine \
&gt;&gt;= return . (c:)<br> <br>
-- or even shorter and still equivalent ;-)<br><br>getMyLine :: IO \
[Char]<br>getMyLine =&nbsp; getChar &gt;&gt;= (\c -&gt; if (c == &#39;\n&#39;) then \
return [] else fmap (c:) getMyLine)<br><br><br><br><div class="gmail_quote">

2008/11/26 abdullah abdul Khadir <span dir="ltr">&lt;<a \
href="mailto:abdullah.ak2002@gmail.com" \
target="_blank">abdullah.ak2002@gmail.com</a>&gt;</span><br><blockquote \
class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt \
0pt 0.8ex; padding-left: 1ex;"> <div><div></div><div class="Wj3C7c">
Hi,<br><br>&nbsp;&nbsp;&nbsp; The function getMyLine written by me is intended for \
getting a complete string from the standard input. <br><br>import IO \
<br><div><div></div><div><br>getMyLine :: IO [Char]<br>

getMyLine =&nbsp; do<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
c &lt;- getChar<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
if(c == &#39;\n&#39;)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
then return &quot;&quot;<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
else&nbsp;&nbsp;&nbsp; cs &lt;- getMyLine<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n \
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
return [c]<br>

<br></div></div>However I keep getting the following error:<br><br>io.hs:14:2: Parse \
error in pattern<br>Failed, modules loaded: \
none.<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; I fail to understand what the \
error is. I tried out various things such as changing the alignment and so on to no \
avail. The following program, however, compiled successfully though it has the same \
structure as the previous program. <br>


<br>checkd :: IO Bool<br>

checkd = do<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
c &lt;- getChar<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
if(c==&#39;d&#39;)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
then&nbsp;&nbsp;&nbsp; return True<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
else&nbsp;&nbsp;&nbsp; return False<br>

<br>
Prelude&gt; :load ./io.hs<br>[1 of 1] Compiling \
Main&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ( io.hs, \
interpreted )<br>Ok, modules loaded: Main.<br>*Main&gt; checkd<br>d<br>True<br><br> \
<br></div></div><div \
class="Ih2E3d">_______________________________________________<br> Haskell mailing \
list<br> <a href="mailto:Haskell@haskell.org" \
target="_blank">Haskell@haskell.org</a><br> <a \
href="http://www.haskell.org/mailman/listinfo/haskell" \
target="_blank">http://www.haskell.org/mailman/listinfo/haskell</a><br> \
<br></div></blockquote></div><br> </blockquote></div><br>



_______________________________________________
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic