【发布时间】:2016-09-16 15:45:12
【问题描述】:
我一直在阅读 Haskell d3js 库:
这是定义 Haskell 盒子的代码:
box :: Selector -> (Double,Double) -> St (Var' Selection)
box parent (w,h) = do
assign
$ ((d3Root
>>> select parent
>>> func "append" [PText "svg"]
>>> width w
>>> height h
>>> style "background" "#eef") :: Chain () Selection)
d3.js 代码中使用box 函数实际导出的代码使用>>= 运算符,如下所示:
import Control.Monad
import qualified Data.Text as T
import D3JS
test :: Int -> IO ()
test n = T.writeFile "generated.js" $ reify (box "#div1" (300,300) >>= bars n 300 (Data1D [100,20,80,60,120]))
为了避免像箭头这样不受欢迎的问题:How to use arrow operators in haskell 我在哪里可以找到类型签名或其他基本信息? 是否有资源可以让我了解更多信息:
-
$Haskell: difference between . (dot) and $ (dollar sign) -
>>>这可能是 arrow 但我看不到我在哪里导入它。 >>=
第一个很容易找到,但答案令人困惑:
*Main Lib> :t ($)
($) :: (a -> b) -> a -> b
我发现f $ a b c = f ( (a b) c ) 而f a b c = (((f a) b) c
Prelude 对涉及单子的>>= 给出了类似的响应。就我而言,它可能是 IO monad。或者 d3js 语句 monad St()
*Main Lib> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
最后一个根本没有出现......这太糟糕了,因为它看起来非常重要
*Main Lib> :t (>>>)
<interactive>:1:1:
Not in scope: ‘>>>’
Perhaps you meant one of these:
‘>>’ (imported from Prelude), ‘>>=’ (imported from Prelude)
最后,冒着一次捆绑太多问题的风险。有人可以解释这种类型签名吗?尤其是最后一项:
box :: Selector -> (Double,Double) -> St (Var' Selection)
【问题讨论】:
-
>>>现在实际上是在Control.Category中定义的。 (Category是Arrow的超类。) -
关于你的最后一个问题——我想请你回答一个反问题——你对此有什么理解——以及什么给你带来了问题。你看过每种类型的定义吗?
-
作为一个不错的技巧,如果你有一个
>>>链,你可以在任何点添加一个“洞”,例如将x1 >>> x2 >>> x3更改为x1 >>> _ >>> x2 >>> x3。这将使 GHC 在编译期间引发 error ,并携带应该在孔中使用的类型。您可以在 Hoogle 上查找该类型,并查看>>>是什么。 (>>=或其他许多事情同上)。 -
@chepner 好的,我在 Hoogle 上找到了
>>>。你知道这里可能使用哪个类别吗?这个箭头>>>会自动知道吗?
标签: haskell monads category-theory