【问题标题】:Nested Lambdas in wxHaskell LibrarywxHaskell 库中的嵌套 Lambda
【发布时间】:2012-11-06 06:57:15
【问题描述】:

我一直试图弄清楚如何使用 wxHaskell 调整 staticText 元素的大小以适应其内容。据我所知,这是 wxWidgets 中的默认行为,但 wxHaskell 包装器专门禁用了此行为。但是,创建新元素的库代码让我很困惑。谁能解释一下这段代码的作用?

staticText :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
staticText parent props
  = feed2 props 0 $
    initialWindow $ \id rect ->
    initialText   $ \txt -> \props flags ->
    do t <- staticTextCreate parent id txt rect flags {- (wxALIGN_LEFT + wxST_NO_AUTORESIZE) -}
       set t props
       return t

我知道feed2 x y f = f x y,而且initialWindow的类型签名是

initialWindow :: (Id -> Rect -> [Prop (Window w)] -> Style -> a) -> [Prop (Window w)] -> Style -> a

initialText 的签名是

initialText :: Textual w => (String -> [Prop w] -> a) -> [Prop w] -> a

但我无法理解所有的 lambda。

【问题讨论】:

    标签: haskell wxwidgets wxhaskell


    【解决方案1】:

    在 Haskell 中,一切都是嵌套的 lambda! \txt -&gt; \props flags -&gt; do {...} 和 \txt props flags -&gt; do {...} 一样,其实都是简写形式

    \txt -> \props -> \flags -> do {...}
    

    这里有点令人困惑的是\txt props flags 似乎有很多论点:

    initialText :: ...=> (String -> [Prop w] -> a) -> ...
    

    似乎采用 2 参数函数,我们给它一个三参数 lambda。但请记住:实际上,每个函数都只需要 一个 参数,其他一切都是通过柯里化完成的。在这种情况下,a 又是一个函数类型,所以它实际上应该改为

    initialText :: Textual w => (String -> [Prop w] -> b -> c) -> [Prop w] -> b -> c
    

    这不是乐趣的结束。 initialWindow 的参数似乎必须是 little 参数,2 而不是 4,但情况又不是这样:我们只给了 initialWindow 它的第一个参数,结果是一个接受的函数更多参数首先是[Prop w] 一个(在这种情况下,initialWindow 的签名中的[Prop(Window w)])。然后它返回a,我们将其重写为b-&gt;c;在这种情况下,initialWindow 需要的是Style -&gt; a。

    所以initialText 在这个应用程序中的实际签名是

    (String -> [Prop(Window w)] -> Style -> c) -> [Prop w] -> Style -> c
    

    【讨论】:

    • 那么flags 是如何设置的?是传递给feed2的0吗?
    • initialText :: (String -> [Prop(Window w)] -> Style -> IO (StaticText ())) -> [Prop w] -> Style -> IO (StaticText () )
    • @kunkelwe: 'flags' 是函数的一个参数,它是传递给 'initialWindow' 的第一个参数。因此,当 'initialWindows' 调用其第一个参数并传递绑定到 'flags' 参数的值时,会设置 'flags'。
    【解决方案2】:

    我没用过的WX库,内部好像使用了奇怪的回调或者延续传递的风格。这个阴影以一种令人困惑的方式props,让我重命名那个傻瓜:

    staticText1 :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
    staticText1 parent propsTopLevel
      = feed2 propsTopLevel 0 $
        initialWindow $ \id rect ->
        initialText   $ \txt -> \propsParam flags ->
        do t <- staticTextCreate parent id txt rect flags
           set t propsParam
           return t
    

    没有 ($) 我可以使用括号:

    staticText2::Window a -> [Prop(StaticText())] -> IO(StaticText()) staticText2 父道具TopLevel = feed2 propsTopLevel 0 (initialWindow (\id rect -> 初始文本 (\txt -> \propsParam 标志 -> 做 t

    可以命名\text -&gt; \props flags -&gt;等lambda:

    staticText3 :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
    staticText3 parent propsTopLevel = initialWindow myWindow propsTopLevel 0
      where makeWindow id rect = initialText myText
              where myText txt propsParam flags = do
                      t <- staticTextCreate parent id txt rect flags
                      set t propsParam
                      return t
    

    在staticText3 中,我使用嵌套词法范围作为参数名称。让我说得更清楚一点:

    staticText4 :: Window a -> [Prop (StaticText ())] -> IO (StaticText ())
    staticText4 = makeWindowTextStatic where
      makeWindowTextStatic parent propsTopLevel = initialWindow (makeTextStatic parent) propsTopLevel 0
      makeTextStatic parent id rect = initialText (makeStatic parent id rect)
      makeStatic parent id rect txt propsParam flags = do
        t <- staticTextCreate parent id txt rect flags
        set t propsParam
        return t
    

    这是否足够清楚以跟随流程?我还没有尝试理解initialWindow 和initialText 本身。

    【讨论】:

      猜你喜欢
      • 2010-12-19
      • 2013-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 2013-02-07
      • 1970-01-01
      相关资源
      最近更新 更多