Error using Prelude.read in Hamlet Yesod - haskell

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?

Related

terraform 11 and "concat" function: parse error

TF v.0.11 (I know it is old but I need this one)
I try to configure output this way:
value = "${concat(aws_lambda_function.lambda.*.arn, [""])}"
The error message is
Error reading config for output FUNCTION_ARN: parse error at 1:46 :
expected expression but found "["
What am I doing wrong, how to fix that?
Reason: [""] don't work for v11.
Solution:
value = "${element(concat(aws_lambda_function.lambda.*.arn, list("")), 0)}"

Invalid Syntax for PEP-8

I'm receiving a notification that advised the below script is not PEP-8:
example_var = print('whoa')
Output:
[E] invalid syntax.
It's showing that the error is a result of the first parentheses in the print statement, but nothing looks off to me.
example_var = print('whoa')
example_var

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.

can snap handle utf8 text? [duplicate]

I have used writeBS writeText from Snap and renderTemplate from heist but none of them seems to support unicode.
site :: Snap ()
site = do
ifTop (writeBS "你好世界") <|>
route [("test", testSnap)]
testSnap :: Snap ()
testSnap = do
fromJust $ C.renderTemplate hs "test"
-- test.tpl
你好世界
I expected it to output "你好世界" for the / or /test route, but in fact its output is just some messy code.
The problem here is not with writeBS or writeText. It's with the conversion used by the OverloadedStrings extension. It is also important to understand the distinction between ByteString and Text. ByteString is for raw bytes. There is no concept of characters or an encoding. That is where Text comes in. The Data.Text.Encoding module has a bunch of functions for converting between Text and ByteString using different encodings. For me, both of the following generate the same output:
writeBS $ encodeUtf8 "你好世界"
writeText "你好世界"
The reason your code didn't work is because your string literal is being converted to ByteString by the OverloadedStrings extension, and it is not giving you the behavior you want. The solution is to treat it as the proper type...Text.
On the Heist side of things, the following works fine for me:
route [("test", cRender "test")]
In fact, this one renders correctly in my browser, while the previous two don't. The difference is that cRender sets an appropriate content-type. I found it enlightening to observe the differences using the following snippet.
site = route [ ("/test1", writeBS "你好世界")
, ("/test2", writeBS $ encodeUtf8 "你好世界")
, ("/test3", writeText "你好世界")
, ("/test4", modifyResponse (setContentType "text/html;charset=utf-8") >> writeText "你好世界")
, ("/testHeist", cRender "test")
]
In my browser test4 and testHeist work correctly. Tests 2 and 3 give you the correct behavior but might not be rendered properly by browsers because of the lack of content-type.

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

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.

Resources