In Haskell,failed to loaded with an error message " Could not find module `IO' " - haskell

I use WinGHCi and my Code(really simple) follows:
module Main
where
import IO
main = do
hSetBuffering stdin LineBuffering
putStrLn "Enter your name: "
name <- getLine
putStrLn("Hello, " ++ name ++ ", how are you?");
error message:
2.hs:4:8:
Could not find module `IO'
It is a member of the hidden package `haskell98-2.0.0.1'.
Use -v to see a list of the files searched for.
Failed, modules loaded: none.
(That codes run correctly in WinHugs, but I just want to compile it)
Maybe the question is really trivial, but I'm study Haskell by myself and nobody can be consulted. I try to search in Google, unfortunately can't find anything meaningful.
I get stuck...Thanks in advance.

You want to import System.IO.

Related

hslogger & Duplicate Log Lines

I've configured logging like so:
import System.Environment
import System.Log.Logger
import System.Log.Handler (setFormatter)
import System.Log.Handler.Simple (streamHandler)
import System.Log.Formatter
import System.IO (getLine, stdout)
main = do
stdOutHandler <- streamHandler stdout DEBUG >>= \lh -> return $
setFormatter lh (simpleLogFormatter "[$time : $loggername : $prio] $msg")
updateGlobalLogger "Linker" (setLevel DEBUG . setHandlers [stdOutHandler])
infoM "Linker" "Hello world!"
Unfortunately, every time I use infoM (or any logging function), I get duplicate lines, e.g.
infoM "Linker" "hi there"
produces:
hi there
[2016-12-05 20:23:10 GMT : Linker : INFO] hi there
I thought setHandlers removed other handlers first.
I want just the lines that are formatted, not the "normal" format ala putStrLn etc.
I found error in your program. Actually, it was in your first code, I just didn't pay enough attention to it :(
All you need is to replace logger name with rootLoggerName in
updateGlobalLogger "Linker"
to
updateGlobalLogger rootLoggerName
This did the trick for me. I don't know what happens when you're not initializing with root logger but now it will at least work.
Also, if you're using stack and don't mind using github projects then you may wish to consider using our logging library (it is not currently on hackage) which is a wrapper around hslogger which adds some juice to it (like coloured logger names and more):
https://github.com/serokell/log-warper

Haskell: simplehttp appending "%0D"?

I am using simplehttp to query webpage. eg: let webLink = "www.example.com/" and number= 257 (number is read from file).
res <- simpleHttp $ "webLink" ++ number
It is working fine on windows but on mac, it is throwing error 404 as its showing path as "www.example.com/257%0D"
I have no idea where this "%0D" is coming from because printing number is giving me 257 . I have tried filtering "%0D" as well like below, but still mac is showing error 404 due to %0D in path...Please suggest.
res <- simpleHttp $ (filter (not . (`elem` "%0D")) ("webLink" ++ number))
The 0x0D character is a component of the newline sequence on windows but not on mac. You are probably reading in a line from your windows-encoded file that contains a windows newline that your mac doesn't understand without a little help from you.

Yesod book example chat and scaffolding

I'm trying to make the chat example from the Yesod book working in the scaffolding site.
I think I've corrected almost all I had to correct, but all of that is completely new to me (it's my first "real" Haskell project) and I'm not very confident in all my modifications; moreover, I'm really stuck at the point 7. Could you comment all the following points if necessary, and helping me for the 7. (in bold some questions/remarks)?
Copy/paste the Chat.hs and Chat/Data.hs in the root of my site,
add import Chat as Import and import Chat.Data as Import to Import.NoFoundation,
add import of IO, Bool, return, Maybe(Nothing), ($) in Data.hs, since the extension NoImplicitPrelude is on Seems very clumsy... Do we have to import all the standard operators on each new file?
in Fundation.hs, add getChat in the App record (after appHttpManager and appLogger)
in Fundation.hs, add YesodChat instance for App: I had to modify the getUserName on the Just uid case (in the original example of Chat, it was just Just uid -> return uid):
Just uid -> do
muser <- runDB $ get uid
case muser of
Nothing -> error "uid not in the DB"
Just user -> return $ userIdent user
This seems very long and nested... Can we do better?
In Fundation.hs, add chatWidget ChatR after the line pc <- widgetToPageContent $ do in the defaultLayout definition.
Now, I have the following warning :
Application.hs:60:36: Warning:
Fields of ‘App’ not initialised: getChat
In the expression: App {..}
I think I have to write something like getChat <- newChan >>=Chat after the appLogger <- newStdoutLoggerSet defaultBufSize >>= makeYesodLogger and appStatic <- ... in the makeFundation definition, but the type doesn't match. I'm a totally lost here, I don't really understand how this function makeFundation works.
You actually got almost the entire way there. I think you just need to change the line to:
getChat <- fmap Chat newChan
Alternatively, if you're not familiar with he fmap function yet, you can use do notation and get:
chan <- newChan
let getChat = Chat chan

Error using Prelude.read in Hamlet Yesod

I get an error when I use the following Haskell code inside Hamlet Yesod:
#{show $ (read "True" :: Bool)}
It seems it doesn't accept ':' characters, but I actually need it.
The error is the following:
Handler/Game.hs:85:11:
Exception when trying to run compile-time code:
"#{show $ read "True" :: Bool}" (line 1, column 25):
unexpected ":"
expecting "}"
Is it possible to bypass this error and be able to use Prelude.read?

Haskell ZeroMQ binding not working for REQ socket

So here i was, barely able to install the libzmq on a windows desktop and then zeromq-haskell with cabal. I wanted to test the api by binding a python program with a haskell program in a hello-world type application.
So the most basic pattern i see is the request-reply pattern . First i tried to make the server in haskell (REP) and the client in python (REQ), witch failed miserably no matter what i did. The generated exception message was Exception: receive: failed (No error).
So i look inside the System.ZMQ and System.ZMQ.Base source code and i see that receive throws an error on calling c_zmq_recv , witch in turn maps directly to a ffi (?) call to the C api. So i think perhaps i didn't do the installation properly , but then i try to make the client in Haskell and the server in python and i notice it works without any problem, so perhaps the recv interface isn't the problem here.
Here is the haskell code below , with both client and server functions
import System.ZMQ
import Control.Monad (forM_,forever)
import Data.ByteString.Char8 (pack,unpack)
import Control.Concurrent (threadDelay)
clientMain :: IO ()
clientMain = withContext 1 (\context->do
putStrLn "Connecting to server"
withSocket context Req $ (\socket-> do
connect socket "tcp://127.0.0.1:5554"
putStrLn $ unwords ["Sending request"]
send socket (pack "Hello...") []
threadDelay (1*1000*1000)
reply<-receive socket []
putStrLn $ unwords ["Received response : ",unpack reply]))
serverMain :: IO ()
serverMain = withContext 1 (\context-> do
putStrLn "Listening at 5554"
withSocket context Rep $ (\socket-> do
connect socket "tcp://127.0.0.1:5554"
forever $ do
message<-receive socket [] -- this throws an IO Exception
putStrLn $ unwords ["Received request : ",unpack message]
threadDelay (1*1000*1000)
send socket (pack "World") [] ))
main :: IO ()
main = serverMain -- replace with clientMain and it works
Now i really didn't get around to testing all other modes of communication (push/pull, subscribe/publish, pair etc.) and for what i need the python server/haskell client is probably better but i am curious about weather i'm doing something wrong or if any part of my code is broken in any way.
Thanks in advance
You need to make one of the sockets (usually the server) bind, you seem to have them both connecting.
Try changing connect socket "tcp://127.0.0.1:5554" to bind socket "tcp://127.0.0.1:5554" in the serverMain function.

Resources