我是 Haskell 的新手,并使用 Scotty 网络库测试了一些概念。
但是,我无法让一个简单的 hello world 页面正常工作。我坚持将参数转换为字符串并应用于另一个函数。
这是尚未工作的高级代码。
{-# 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
错误信息
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>"]
| ^^^^^^^^
目标
该hello
函数是一个存根。我想证明以下机制有效。
将参数提取为字符串
应用于
String -> String
函数将结果作为响应返回
我读过什么并尝试过什么
我读到param
is of typeParsable a => Text -> ActionM a
并且ActionM
is of type ActionT Text IO
。
我试过name :: T.Text <- param "name"
, T.unpack
,liftIO
等,但没有运气。我想我不完全理解这些类型。
问题
这些类型的用途param
和ActionM
实际含义是什么?
如何将参数提取为字符串以与其他函数一起使用?
谢谢你。