[Haskell-beginners] Re: [Haskell] Re: Help : A problem with IO

abdullah abdul Khadir abdullah.ak2002 at gmail.com
Thu Nov 27 13:10:57 EST 2008


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 at 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 at 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 at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20081127/d7715dac/attachment.htm


More information about the Beginners mailing list