小编Ski*_*gys的帖子

Text.Printf与Data.Text?

Data.Text在打印出来进行调试之前我一直在解压缩实例,并且认为只是用Text.Printf它.不幸的是,我无法让它发挥作用:

{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
import Data.Text
import Text.Printf

--instance PrintfArg Text where
--  toUPrintf = toUPrintf . unpack

main :: IO ()
main = do
  let input :: Text = "abc"
  printf "Input: %s\n" input
Run Code Online (Sandbox Code Playgroud)

错误:

src/Main.hs:12:3:
    No instance for (PrintfArg Text)
      arising from a use of `printf'
    Possible fix: add an instance declaration for (PrintfArg Text)
    In a stmt of a 'do' block: printf "Input: %s" input
    In the expression:
      do …
Run Code Online (Sandbox Code Playgroud)

haskell

17
推荐指数
1
解决办法
4612
查看次数

IO和也许monad互动

我有以下代码,但我觉得它太丑陋而且势在必行.有人会说它更具功能吗?(我和MaybeT混淆但是无法使它工作)也欢迎应用答案.

getString :: IO String

pred :: String -> Bool

f :: String -> String

result :: IO (Maybe String)
result = do
  s <- getString
  if pred s
    then return $ Just $ f s
    else return Nothing
Run Code Online (Sandbox Code Playgroud)

编辑:一个后续问题:如果pred和f都返回IO内的结果会怎么样(我应该把它分成一个单独的问题吗?)

getString :: IO String

pred :: String -> IO Bool

f :: String -> IO String

result :: IO (Maybe String)
result = do
  s <- getString
  b <- pred s
  if b
    then Just <$> f s
    else return …
Run Code Online (Sandbox Code Playgroud)

haskell

16
推荐指数
4
解决办法
6796
查看次数

Haskell中的验证

我有一些我需要验证的嵌套记录,我想知道什么是惯用的Haskell方法.

简化:

data Record = Record {
  recordItemsA :: [ItemA],
  recordItemB :: ItemB
} deriving (Show)

data ItemA {
  itemAItemsC :: [ItemC]
} deriving (Show)
Run Code Online (Sandbox Code Playgroud)

要求是:

  • 收集并返回所有验证错误
  • 某些验证可能跨项目,例如ItemsA反对ItemB
  • Strings足以表示错误

我目前的代码感觉很尴尬:

type ErrorMsg = String

validate :: Record -> [ErrorMsg]
validate record =
  recordValidations ++ itemAValidations ++ itemBValidations
  where
    recordValidations :: [ErrorMsg]
    recordValidations = ensure (...) $
      "Invalid combination: " ++ (show $ recordItemsA record) ++ " and " ++ (show $ recordItemsB record)
    itemAValidations :: [ErrorMsg] …
Run Code Online (Sandbox Code Playgroud)

haskell

10
推荐指数
2
解决办法
1571
查看次数

忽略Control.Applicative中的参数

我正在编写一个xml-conduit解析器,我更喜欢使用monadic的应用语法.有很多论据可以结合起来,但我在应用程序中有些迷失了.我当前的问题是8个参数,我只想使用第4个和第6个来构造结果.

我能使它发挥作用的唯一方法是:虽然平面解决方案应该有奇特的星星排列:

import Control.Applicative

a1 :: Applicative Text
a2 :: Applicative Text
a3 :: Applicative Text
a4 :: Applicative Text
a5 :: Applicative Text
a6 :: Applicative Text
a7 :: Applicative Text
a8 :: Applicative Text

data Data = Data Text Text
f :: Text -> Text -> Data

parser :: Applicative Data
parser = a1 *> a2 *> a3 *> (f <$> a4 <* a5 <*> a6) <* a7 <* a8
Run Code Online (Sandbox Code Playgroud)

有没有括号的形式的任何方式?

parser = f <$> a1 ?? a2 …
Run Code Online (Sandbox Code Playgroud)

haskell applicative

6
推荐指数
1
解决办法
177
查看次数

更简单的groupStartBy函数?

我在第二次在不相关的项目中编写以下函数(首先是XML处理,现在是自定义命令行标记处理),我感觉它应该存在于某个库中,我只是找不到它.它对列表元素进行分组,每个组从谓词为真的元素开始.

有任何更简单的方法吗?

groupStartBy :: (a -> Bool) -> [a] -> [[a]]
groupStartBy pred xs = reverse $ map reverse $ foldl' step [] xs
  where
    step as x | pred x = [x]:as
    step (a:as) x = (x:a):as
    step [] x = [[x]]
Run Code Online (Sandbox Code Playgroud)

haskell

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

Haskell数据结构与键的高效不精确查找?

我有数据键入Data.Time.Calendar.Day并需要有效地查找它.有些日期丢失,当我尝试通过缺少的密钥查找时,我希望将数据附加到最近的现有密钥,有点像std :: map :: lower_bound.

对可以执行此操作的现有库的任何建议?我搜索了一会儿,只找到支持精确密钥查找的地图.

谢谢.

haskell

0
推荐指数
1
解决办法
127
查看次数

标签 统计

haskell ×6

applicative ×1