我需要创建一个web服务来使用Haskell中的 scotty web framework 在不同货币之间进行转换。
web服务应该响应get请求,如/convert/15? to =usd&from=eur。
到目前为止,我有以下代码:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid (mconcat)
functionxs :: a -> Int
functionxs a = 5
main = scotty 3000 $ do
get "/:word" $ do
name <- param "word"
html $ mconcat ["<h1>Hi, ", name, " what's up!</h1>"]因此,当您在浏览器中执行:http://localhost:3000/Tony时,结果是:嗨,托尼,怎么了!
问题是我不知道如何更改代码以使'/convert/15? to =usd&from=eur.‘并得到正确的答案。
希望有人能帮我。
提前谢谢。
使用最终解决方案进行编辑:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid (mconcat)
import Data.Text.Lazy (Text, pack)
main = scotty 3000 $ do
get "/convert/:amount" $ do
money <- param "amount"
to <- param "to"
frm <- param "from"
html $ mconcat ["<h1>The result is: ", pack (show (convert
money to frm)), "</h1>"]
convert :: Double -> String -> String -> Double
convert a b c = if (b=="euro" && c=="usd")
then (a*1.091)
else if (b=="usd" && c=="euro")
then (a*0.915)
else 0发布于 2017-05-01 20:10:23
查看docs,您需要调用param来获取所需的内容。
试着以此为起点:
{-# LANGUAGE OverloadedStrings #-}
import Web.Scotty
import Data.Monoid
main = scotty 3000 $ do
get "/convert/:amt" $ do
amt <- param "amt"
frm <- param "from"
to <- param "to"
html $ "<h1>" <> amt <>" in " <> frm <> " is " <> to <> "</h1>"我会把转换的部分留给你去弄清楚。使用<>而不是mconcat看起来也更干净。
https://stackoverflow.com/questions/43716497
复制相似问题