重构 Haskell lambda 函数

发布于 2025-01-14 23:14:18 字数 1341 浏览 2 评论 0原文

我有一个 lambda 函数 ((:) . ((:) x)) ,我将其传递给 foldr ,如下所示: foldr ((:) . ( (:) x)) [] xs 其中 xs 是二维列表。我想重构以使其更清晰(这样我可以更好地理解它)。我想它会像这样完成:

foldr (\ element acc -> (element:acc) . (x:acc)) [] xs

但这给了我错误:

ex.hs:20:84: error:
    • Couldn't match expected type ‘a0 -> b0’ with actual type ‘[[a]]’
    • Possible cause: ‘(:)’ is applied to too many arguments
      In the second argument of ‘(.)’, namely ‘(x : acc)’
      In the expression: (element : acc) . (x : acc)
      In the first argument of ‘foldr’, namely
        ‘(\ element acc -> (element : acc) . (x : acc))’
    • Relevant bindings include
        acc :: [[a]] (bound at ex.hs:20:60)
        element :: [a] (bound at ex.hs:20:52)
        xs :: [[a]] (bound at ex.hs:20:30)
        x :: [a] (bound at ex.hs:20:28)
        prefixes :: [a] -> [[a]] (bound at ex.hs:20:1)
   |
20 | prefixes = foldr (\x xs -> [x] : (foldr (\ element acc -> (element:acc) . (x:acc)) [] xs)) []
   |  

编辑:我围绕此片段的所有相关代码是

prefixes :: Num a => [a] -> [[a]]
prefixes = foldr (\x acc -> [x] : (foldr ((:) . ((:) x)) [] acc)) []

及其调用是:

prefixes [1, 2, 3]

如何重构 lambda ((:) 。((:) x )) 包含它的两个参数?

I have a lambda function ((:) . ((:) x)) that I am passing to foldr like so: foldr ((:) . ((:) x)) [] xs where xs is a 2d list. I would like to refactor to make it clearer (so I can better understand it). I imagine it would be done like so:

foldr (\ element acc -> (element:acc) . (x:acc)) [] xs

But this gives me the error:

ex.hs:20:84: error:
    • Couldn't match expected type ‘a0 -> b0’ with actual type ‘[[a]]’
    • Possible cause: ‘(:)’ is applied to too many arguments
      In the second argument of ‘(.)’, namely ‘(x : acc)’
      In the expression: (element : acc) . (x : acc)
      In the first argument of ‘foldr’, namely
        ‘(\ element acc -> (element : acc) . (x : acc))’
    • Relevant bindings include
        acc :: [[a]] (bound at ex.hs:20:60)
        element :: [a] (bound at ex.hs:20:52)
        xs :: [[a]] (bound at ex.hs:20:30)
        x :: [a] (bound at ex.hs:20:28)
        prefixes :: [a] -> [[a]] (bound at ex.hs:20:1)
   |
20 | prefixes = foldr (\x xs -> [x] : (foldr (\ element acc -> (element:acc) . (x:acc)) [] xs)) []
   |  

Edit: My all relevant code surrounding this snippet is

prefixes :: Num a => [a] -> [[a]]
prefixes = foldr (\x acc -> [x] : (foldr ((:) . ((:) x)) [] acc)) []

and its invocation is:

prefixes [1, 2, 3]

How can I refactor the lambda ((:) . ((:) x)) to include both its arguments?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林很绿却致人迷途 2025-01-21 23:14:18

您可以逐步将其转换为 lambda。

(:) . ((:) x)
\y -> ((:) . (((:) x)) y  -- conversion to lambda
\y -> (:) (((:) x) y)     -- definition of (.)
\y -> (:) (x : y)         -- rewrite second (:) using infix notation
\y z -> (:) (x : y) z     -- add another parameter
\y z -> (x : y) : z       -- rewrite first (:) using infix notation

You can step-by-step convert it to a lambda.

(:) . ((:) x)
\y -> ((:) . (((:) x)) y  -- conversion to lambda
\y -> (:) (((:) x) y)     -- definition of (.)
\y -> (:) (x : y)         -- rewrite second (:) using infix notation
\y z -> (:) (x : y) z     -- add another parameter
\y z -> (x : y) : z       -- rewrite first (:) using infix notation
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文