[Haskell-beginners] GHC not buying what I am offering this afternoon

Joel Williamson joel.s.williamson at gmail.com
Mon Mar 9 20:39:37 UTC 2015


sequence will get the types to match up, but a more elegant solution
would be to get every line into a single string, then print that.

putStrLn $ unlines $ map show newList

I agree that getting the types to line up can be a nuisance, and with
such small programs it doesn't bring much of an advantage. Ultimately,
if you want side-effects to be reflected in the type system, there
will be times you have to do a bit of extra work to satisfy the type
checker. Learning to write well-typed code is much easier in this
context, where everything has a fairly concrete type and there is lots
of documentation, than having to learn it later when you have weird
types coming from 3 different libraries and are facing a problem no
one else has had. I would recommend getting very comfortable with GHCi
and Hoogle.

If you haven't already, add a hoogle prompt to GHCi by pasting something like
:def hoogle \str -> return $ ":! hoogle --count=15 \"" ++ str ++ "\""
in your ghci.conf. This will allow you to easily search for functions
of a given type. Typing
:hoogle [IO a] -> IO [a]
returned all the information needed to answer your question.


On Mon, Mar 9, 2015 at 4:25 PM, Geoffrey Bays <charioteer7 at gmail.com> wrote:
> Thanks, Joel.
>
> Putting the type IO [()] in the main declaration and this as the final line
> of the main function does do the trick:
>
> sequence [putStrLn $ show s | s <- newList]
>
> But this is the kind of thing that makes Haskell types difficult for
> beginners to work with...
>
> Geoffrey
>
> On Mon, Mar 9, 2015 at 4:15 PM, Joel Williamson
> <joel.s.williamson at gmail.com> wrote:
>>
>> main must have type IO a. Hoogle tells me that to convert [IO a] -> IO
>> [a], you should use the function sequence. Try applying that to your final
>> line.
>>
>>
>> On Mon, 9 Mar 2015 16:07 Geoffrey Bays <charioteer7 at gmail.com> wrote:
>>>
>>> My main function looks like this:
>>>
>>> main :: [IO()]
>>> main = do
>>>     let stud1 = Student {name = "Geoff", average = -99.0, grades =
>>> [66,77,88]}
>>>     let stud2 = Student {name = "Doug", average = -99.0, grades =
>>> [77,88,99]}
>>>     let stud3 = Student {name = "Ron", average = -99.0, grades =
>>> [55,66,77]}
>>>     let studList = [stud1,stud2]
>>>     let newList = calcAvg studList
>>>     [putStrLn $ show s | s <- newList]
>>>     --putStrLn $ show (newList !! 0)
>>>     --putStrLn $ show (newList !! 1)
>>>
>>> With this final line, putStrLn $ show (newList !! 0), the type IO () in
>>> the function declaration compiles fine.
>>> But with [putStrLn $ show s | s <- newList] as the final line, [IO ()] in
>>> the function declaration will not compile, I get this error:
>>>
>>>     Couldn't match expected type `IO t0' with actual type `[IO ()]'
>>>
>>> What does the declared type need to be for a final line of:
>>> [putStrLn $ show s | s <- newList]  ???
>>>
>>> Thanks,
>>>
>>> Geoffrey
>>>
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>


More information about the Beginners mailing list