【发布时间】:2023-09-10 08:52:01
【问题描述】:
我是 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函数将结果作为响应返回
我阅读和尝试了什么
我读过 Scotty doc 和一些 code examples。
我读到param 是Parsable a => Text -> ActionM a 类型,ActionM 是ActionT Text IO 类型。
我尝试过name :: T.Text <- param "name"、T.unpack、liftIO 等,但没有运气。我想我不完全了解这些类型。
问题
param 和 ActionM 的类型实际上是什么意思?
如何将参数提取为字符串以与其他函数一起使用?
谢谢。
【问题讨论】:
-
您的错误信息是什么?专注于这些是达成解决方案的好方法。
-
另外,当您说“这些类型实际上是什么意思?”时,您想解释哪些类型?我已经确定了几个。
-
@MichaelLitchard 嗨,迈克尔,我已经更新了错误消息。我的意思是
param和ActionM的类型。谢谢!