小编Nei*_*eil的帖子

IO in where子句

我以为我开始理解Haskell中的IO,直到遇到以下问题.

我有以下函数,它返回类型IO Float:

getFundPrice :: Int ->  Int -> IO Float
getFundPrice fund date = do 
                       priceList <- getFundPrice' fund date
                       let h = head priceList
                       return  h
Run Code Online (Sandbox Code Playgroud)

函数getFundPrice'使用takusen数据库库并返回类型IO [Float]的列表.

我可以使用以下方法使用Hunit成功测试getFundPrice函数:

  p <- getFundPrice 120 20100303
  assertEqual
    "get fund price"
    10.286 
    (p)
Run Code Online (Sandbox Code Playgroud)

困扰我的问题是以下函数定义:

lastPos :: Int -> (Shares,Float) -> Period -> Fund -> (Shares,Float)
lastPos endDate begPos [] fund  = (fst begPos, trnPrice) 
                               where trnPrice = do
                                             price <- getFundPrice fund endDate
                                             return price                                                                        
Run Code Online (Sandbox Code Playgroud)

我在尝试编译时遇到的错误是"无法匹配预期类型Float' against inferred typeIO …

io monads haskell

5
推荐指数
2
解决办法
514
查看次数

从haskeline中调用网络IO

我有一个现有的程序,它接受命令行参数(用户名,密码,日期),然后使用该Network.HTTP.Conduit库将xml消息发布到服务器.然后我解析结果,做一些工作并使用blaze-html写出文件.

这一切都像一个魅力; 但是,我以为我会使用haskeline密码不可见.我能够创建一个命令行程序来获取用户提供的值并打印出来,但是如果我调用使用管道的函数,它就永远不会返回.

这是违规代码:

main = runInputT defaultSettings loop
where 
    loop :: InputT IO ()
    loop = do
        Just username <- getInputLine "WM username: "
        Just password <- getPassword (Just '*') "WM password: "
        Just date     <- getInputLine "Date (YYYYMMDD): "

        outputStrLn "querying WM..."
        clients <- lift $ getWMClients username password
        outputStrLn "successfully retrieved client list from WM..."

        let outHeader = renderHeader date username

        reportString <- mapM  (\x -> createString x clients)  cList

        lift $ writeFile …
Run Code Online (Sandbox Code Playgroud)

haskell haskeline http-conduit

5
推荐指数
1
解决办法
361
查看次数

将http-conduit连接到xml-conduit

我正在努力通过xml-conduit将响应从http-conduit转换为XML文档.

doPost函数接受XML Document并将其发布到服务器.服务器使用XML文档进行响应.

doPost queryDoc = do
    runResourceT $ do

        manager <- liftIO $ newManager def
        req <- liftIO $ parseUrl hostname

        let req2 = req
                { method = H.methodPost
                , requestHeaders = [(CI.mk $ fromString "Content-Type", fromString "text/xml" :: Ascii) :: Header] 
                , redirectCount = 0
                , checkStatus = \_ _ -> Nothing
                , requestBody =  RequestBodyLBS $ (renderLBS def queryDoc)
                }

        res <- http req2 manager
        return $ res
Run Code Online (Sandbox Code Playgroud)

以下工作并返回'200':

let pingdoc = Document (Prologue [] …
Run Code Online (Sandbox Code Playgroud)

haskell conduit http-conduit

4
推荐指数
1
解决办法
698
查看次数

如何使用http-enumerator处理异常

Haskell新手在这里.我正在尝试使用http-enumerator通过HTTP上的XML查询服务.我能够连接并发送xml格式的请求,并接收xml格式的响应.

查询成功后,服务器将发回一个响应

我正在努力的是处理FAULT文档中指出的异常的正确方法.我正在尝试使用Either,但没有成功.

我在下面编写的ghci:

import Network.HTTP.Enumerator 
import Network.HTTP.Types

import qualified Data.ByteString.Lazy as L 
import Data.ByteString.UTF8

import Text.XML.Light

hostname = "https://server..."

doPost username password token = do

    let q = QName "SYSTEM" Nothing Nothing

    let attribs = [Attr {attrKey = QName "user" Nothing Nothing, attrVal = username},
                   Attr {attrKey = QName "password" Nothing Nothing, attrVal = password},
                   Attr {attrKey = QName "token" Nothing Nothing, attrVal = token}]

    let doc = Element {elName=q, elAttribs=attribs, elContent= [], elLine=Nothing}

    req0 <- parseUrl …
Run Code Online (Sandbox Code Playgroud)

haskell

2
推荐指数
1
解决办法
207
查看次数

链接到Yesod 0.9.3.2中的静态css

我正在关注yesod wiki上的yesod教程,并点击链接到外部css框架(蓝图)的墙.

我使用脚手架工具创建了网站,到目前为止,所有内容都使用'yesod devel'正常工作.

我已将蓝图文件下载到static/css/blueprint中,并将以下内容添加到default-layout-wrapper.hamlet:

!!!
<html>
    <head
        <title>#{pageTitle pc}
        <link rel=stylesheet type=text/css media=screen href=@{StaticR css_blueprint_screen_css}>
        <link rel=stylesheet type=text/css media=print href=@{StaticR css_blueprint_print_css}>
        ^{pageHead pc}
    <body
        ^{pageBody pc}
Run Code Online (Sandbox Code Playgroud)

我得到的错误是:

Foundation.hs:98:27:
    Not in scope: `css_blueprint_screen_css'
    In the result of the splice:
      $(hamletFile "hamlet/default-layout-wrapper.hamlet")
    To see what the splice expanded to, use -ddump-splices
    In the first argument of `hamletToRepHtml', namely
      `$(hamletFile "hamlet/default-layout-wrapper.hamlet")'
    In the expression:
      hamletToRepHtml
        ($(hamletFile "hamlet/default-layout-wrapper.hamlet"))

Foundation.hs:98:27:
    Not in scope: `css_blueprint_print_css'
    In the result of the splice:
      $(hamletFile "hamlet/default-layout-wrapper.hamlet")
    To see …
Run Code Online (Sandbox Code Playgroud)

yesod

2
推荐指数
1
解决办法
820
查看次数

标签 统计

haskell ×4

http-conduit ×2

conduit ×1

haskeline ×1

io ×1

monads ×1

yesod ×1