I am new to Haskell and testing some concepts with Scotty web library.
However, I can't get a simple hello world page working. I am stuck at converting the parameter as String and apply to another function.
Here is the high-level code that is not working yet.
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Web.Scotty
main :: IO ()
main = scotty 3000 $
get "/" $ do
name <- param "name" `rescue` (\_ -> return "haskell")
greeting <- hello name
html $ concat ["<h1>hello ", greeting, "</h1>"]
hello :: String -> String
hello s = "hello " ++ s
Error Messages
app/Main.hs:11:17: error:
• Couldn't match type ‘[]’
with ‘Web.Scotty.Internal.Types.ActionT
Data.Text.Internal.Lazy.Text IO’
Expected type: Web.Scotty.Internal.Types.ActionT
Data.Text.Internal.Lazy.Text IO Char
Actual type: String
<Omitted>
|
11 | greeting <- hello name
| ^^^^^^^^^^
app/Main.hs:12:12: error:
• Couldn't match expected type ‘Data.Text.Internal.Lazy.Text’
with actual type ‘[Char]’
<Omitted>
|
12 | html $ concat ["<h1>hello ", greeting, "</h1>"]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
app/Main.hs:12:34: error:
• Couldn't match expected type ‘[Char]’ with actual type ‘Char’
<Omitted>
|
12 | html $ concat ["<h1>hello ", greeting, "</h1>"]
| ^^^^^^^^
Goal
The hello
function is a stub. I would like to proof that the following mechanism work.
extract a parameter as String
apply to a
String -> String
functionreturn the result as response
What have I read and tried
I have read the Scotty doc and some code examples.
I read that the param
is of type Parsable a => Text -> ActionM a
and ActionM
is of type ActionT Text IO
.
I have tried name :: T.Text <- param "name"
, T.unpack
, liftIO
, etc. but no luck. I think I don't understand the types fully.
Questions
What do the types for param
and ActionM
actually mean?
How can I extract the parameter as String to use with another functions?
Thank you.
param
andActionM
. Thanks! – Gavin