[Haskell-beginners] monads do not fit together?

Kees Bleijenberg k.bleijenberg at lijbrandt.nl
Mon Oct 14 14:58:47 UTC 2013


Kim,

 

I simplified the problem by removing the if then. After that I desugared the
do block to what I think is the equivalent with >> and >>=.

I get the same message (Couldn't match expected type `CGIT IO a0'   with
actual type `IO [String]'). I think I do understand where this message is
coming from. But what can I do to fix it?

 

Kees

  

 

module Main(

   main

)

 

where 

 

import Network.CGI

import Text.XHtml

import Text.XHtml.Transitional

import Data.Maybe

 

runRefreshFirewall ::  String -> IO [String]

runRefreshFirewall un =  do

                               return ["test" ]      

                                                      

inputCgiOkPage :: String -> [String] -> Html 

inputCgiOkPage un  msgs = body << h1 << ("Un: " ++ un)


 

{--

cgiMain :: CGI CGIResult   

cgiMain = do

            maybeUn <- getInput "un"            

            do 

              msgs <-runRefreshFirewall (fromJust maybeUn) 

              output $ renderHtml $ (inputCgiOkPage (fromJust maybeUn) msgs)

--}

 

 

cgiMain :: CGI CGIResult   

cgiMain = (getInput "un") >>= \maybeUn -> (runRefreshFirewall (fromJust
maybeUn) >>= 

                              \msgs -> (output $ renderHtml $ inputCgiOkPage
(fromJust maybeUn) msgs))

 

main :: IO ()

main = runCGI $ handleErrors cgiMain

 

 

 

Van: Beginners [mailto:beginners-bounces at haskell.org] Namens Kim-Ee Yeoh
Verzonden: maandag 14 oktober 2013 14:58
Aan: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell
Onderwerp: Re: [Haskell-beginners] monads do not fit together?

 

 

On Mon, Oct 14, 2013 at 6:51 PM, Kees Bleijenberg
<k.bleijenberg at lijbrandt.nl> wrote:

The error at the problem line is:  parse error on input `<-' 

If I move the line above the 'if' I get: Couldn't match expected type `CGIT
IO t0' with actual type `IO [String]'.   

 

Looks like you're missing a "do". You're writing code like this:

if blah then

  x <- foo

  bar x

else

  quux

when you need to write

if blah then do

  x <- foo

  bar x

else

  quux

 

Recommended exercises:


* can you see in your mind's eye the desugaring of do-syntax?


* how would you write it in a single line, i.e. without using indentation
a.k.a. "layout" -- hint: look up curly braces

 

-- Kim-Ee

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20131014/a898241c/attachment.html>


More information about the Beginners mailing list