导致此错误的原因是什么?为什么我会认为它破坏了参照透明性又为什么呢?

迈克·HR

很抱歉提出这个问题,但是在我看来,它破坏了参照透明性。

在研究问题并将其分解(问题是获得列表列表的对角线元素)时,我想到了这个(正确工作)的解决方案:

import Data.List

nums = [[1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14,15,16]]

southwest = transpose . zipWith (drop) [0..]
southwest2 = transpose . zipWith (drop) [0..] . transpose

my_ans = southwest nums ++ southwest2 nums

(注意,它两次包含中间行,这对我的用例来说不是问题)

现在显然可以很容易地对其进行重构。在首次尝试以无点样式编写它之后,我发现通过这些尝试简单地以非无点形式编写它会更容易:

my_ans2 x = (diagFunc x) ++ (diagFunc . transpose x)
              where diagFunc = transpose . zipWith (drop) [0..]

my_ans3 x = concat [(diagFunc x), (diagFunc . transpose x)]
              where diagFunc = transpose . zipWith (drop) [0..]

现在,这些编译和工作都没有使我感到困惑,因为这似乎会破坏参照透明性。有人可以解释为什么我对此有误,以及如何正确编写此函数(加分点是我无法做到的,以无点格式编写)。

供参考,错误在这里:

/home/michael/scripts/temp.hs:14:30:
    Couldn't match expected type ‘[[a]]’
                with actual type ‘a0 -> [[a1]]’
    Relevant bindings include
      x :: [[a]]
        (bound at /home/michael/scripts/project_euler/temp.hs:14:9)
      my_ans2 :: [[a]] -> [[a]]
        (bound at /home/michael/scripts/project_euler/temp.hs:14:1)
    In the second argument of ‘(++)’, namely ‘(diagFunc . transpose x)’
    In the expression: (diagFunc x) ++ (diagFunc . transpose x)
    In an equation for ‘my_ans2’:
        my_ans2 x
          = (diagFunc x) ++ (diagFunc . transpose x)
          where
              diagFunc = transpose . zipWith (drop) [0 .. ]

/home/michael/scripts/temp.hs:14:41:
    Couldn't match expected type ‘a0 -> [[a1]]’
                with actual type ‘[[a]]’
    Relevant bindings include
      x :: [[a]]
        (bound at /home/michael/scripts/project_euler/temp.hs:14:9)
      my_ans2 :: [[a]] -> [[a]]
        (bound at /home/michael/scripts/project_euler/temp.hs:14:1)
    Possible cause: ‘transpose’ is applied to too many arguments
    In the second argument of ‘(.)’, namely ‘transpose x’
    In the second argument of ‘(++)’, namely ‘(diagFunc . transpose x)’

同样,我非常确定我对此有误,我只需要有人指出如何做即可。:) 提前致谢。

背风处

您可以使用以下方法修复它:

my_ans2 x = (diagFunc x) ++ (diagFunc . transpose $ x)
              where diagFunc = transpose . zipWith (drop) [0..]

my_ans3 x = concat [(diagFunc x), (diagFunc . transpose $ x)]
              where diagFunc = transpose . zipWith (drop) [0..]

问题是将diagFunc . transpose x其解析为diagFunc . (transpose x),而不是(diagFunc . transpose) x您期望的那样。

您可以使用函数的monoid实例以无点样式编写此代码:

import Data.Monoid

my_ans2' = diagFunc `mappend` (diagFunc . transpose)
  where diagFunc = transpose . zipWith (drop) [0..]

function的monoid实例可用于返回monoidal值的函数。由于列表是monoid,其中mappend=(++)可以在这里使用。它定义为:

instance Monoid b => Monoid (a -> b) where
    mempty _ = mempty
    mappend f g x = f x `mappend` g x

因此,mappend对于函数fgapplyfg论点x,并将结果组合在一起使用mappend于monoid类型bb这是一个列表,所以您最终得到

my_ans2' = (f x) ++ (g x)

f现在diagFuncg现在在哪里diagFunc . transpose

正如@chi在评论中指出的,您还可以使用适用的解决方案:

import Control.Applicative

my_ans2'' = (++) <$> diagFunc <*> (diagFunc . transpose)
  where diagFunc = transpose . zipWith (drop) [0..]

这是更通用的,因为它不依赖于两个函数的返回类型为monoid。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么serialize()破坏了我的更新?

来自分类Dev

为什么我会遇到断点编译错误

来自分类Dev

为什么我会从WinLess收到此错误消息?

来自分类Dev

为什么我会遇到断点编译错误

来自分类Dev

为什么我会从WinLess收到此错误消息?

来自分类Dev

SQL 错误为什么我会“缺少右括号”?

来自分类Dev

为什么Matlab的透明违反了透明性?

来自分类Dev

为什么我会变空

来自分类Dev

为什么我会“丢失”;前 '*' ”?

来自分类Dev

jQuery为什么会破坏此代码?

来自分类Dev

为什么会导致错误?

来自分类Dev

解析错误-导致错误的原因是什么?

来自分类Dev

解析错误-导致错误的原因是什么?

来自分类Dev

核心数据“数据库似乎已损坏”-导致此错误的原因是什么?

来自分类Dev

核心数据“数据库似乎已损坏”-导致此错误的原因是什么?

来自分类Dev

为什么从JavaScript算起我会误认为小时?

来自分类Dev

为什么透明材料会导致咬合?

来自分类Dev

JSON中此错误的可能原因是什么?

来自分类Dev

此BigQuery搜索错误的原因是什么?

来自分类Dev

JSON中此错误的可能原因是什么?

来自分类Dev

为什么此代码会导致分段错误

来自分类Dev

为什么此代码会导致“需要对象”错误?

来自分类Dev

为什么此XSD验证会导致错误

来自分类Dev

为什么此功能会导致段错误?

来自分类Dev

为什么此表达式会导致SQL错误?

来自分类Dev

为什么此代码会导致分段错误

来自分类Dev

为什么此代码会导致“需要对象”错误?

来自分类Dev

为什么此流功能会导致错误?

来自分类Dev

为什么此XSD验证会导致错误

Related 相关文章

热门标签

归档