【发布时间】:2014-08-11 08:38:54
【问题描述】:
我将this code 的第一个版本转为使用StateT L8.ByteString Maybe a。到目前为止,我已经把大部分功能变成了这个
matchHeader :: L8.ByteString -> StateT L8.ByteString Maybe ()
matchHeader prefix = StateT $ \str ->
if prefix `L8.isPrefixOf` str
then Just ((), L8.drop (L8.length prefix) str)
else Nothing
getNat :: Num a => StateT L8.ByteString Maybe a
getNat = StateT $ \str ->
case L8.readInt str of
Nothing -> Nothing
Just (num, rest)
| num <= 0 -> Nothing
| otherwise -> Just (fromIntegral num, rest)
getBytes :: Integer -> StateT L8.ByteString Maybe L8.ByteString
getBytes n = StateT $ \str ->
let
count = fromIntegral n
both@(prefix, _) = L8.splitAt count str
in if L8.length prefix < count
then Nothing
else Just both
但是如何在不使用 lambda 表达式的情况下编写这些?我尝试了一些变化。到目前为止没有运气。
【问题讨论】:
-
你可以给函数命名。您可以使用 lambda-case 扩展。
-
为什么要在没有 lambda 表达式的情况下编写它们?
-
学习目的
标签: haskell lambda monad-transformers