Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 哈斯凯尔的守卫与纯洁_Haskell - Fatal编程技术网

Haskell 哈斯凯尔的守卫与纯洁

Haskell 哈斯凯尔的守卫与纯洁,haskell,Haskell,我在读,有这样一个代码: buy n = r!n where r = listArray (0,n) (Just (0,0,0) : map f [1..n]) f i = do (x,y,z) <- attempt (i-6) return (x+1,y,z) `mplus` do (x,y,z) <- attempt (i-9)

我在读,有这样一个代码:

buy n = r!n
    where r = listArray (0,n) (Just (0,0,0) : map f [1..n])
          f i = do (x,y,z) <- attempt (i-6)
                   return (x+1,y,z)
                `mplus`
                do (x,y,z) <- attempt (i-9)
                   return (x,y+1,z)
                `mplus`
                do (x,y,z) <- attempt (i-20)
                   return (x,y,z+1)
          attempt x = guard (x>=0) >> r!x
因此,如果x>0,则:

attempt x
    = (guard True) >> (r!x) = (pure ()) >> (r!x)
    = (pure ()) >>= \_ -> r!x = (f ()) >>= (\_ -> r!x)
因此
f()
应该是
ma
类型(在这种情况下
可能是a
),但是Haskell怎么知道
f
是什么
f()
可能返回
empty
,因为它从未被指定过。(
f
在纯文本中表示
f


如果x>=

这是一个多问题的问题,但让我们看看我能否把事情说得更清楚一点

当解释
pure()
时,Haskell如何知道
f
是什么
pure
是一个类型类方法,因此它只是来自我们所处类型的实例声明。这一点最近发生了变化,因此您可能必须遵循不同的路径才能找到答案,但结果是相同的:

以同样的方式

通过在ghci提示符下键入
:i pure
:i empty
,您将了解typeclass提供了哪些函数;然后,您可以查找
可能
为它们创建的实例声明


不幸的是,从这样一个角度来看,这种情况最近发生了变化,因此在不知道您使用的具体版本的情况下,没有明确的永久性答案。希望这会很快解决。

在手动计算
尝试x的最后一个表达式中,您正在混淆类型和值<代码>纯::a->f a
不是定义;它是一个类型签名(请注意
)。完整地说,
pure
的类型是:

GHCi> :t pure
pure :: Applicative f => a -> f a
这里,
f
代表
Applicative
的任何实例,
a
代表任何类型。在您的例子中,您使用的是
Maybe
monad/applicative functor,因此
f
Maybe
pure()
的类型是
Maybe()
。(
()::()
是当您对结果不感兴趣时使用的伪值。
pure()
中的
()
是一个值,但是
中的
()
可能()
是一个类型-
()
值的类型)

我们将从您评估的最后一个正确步骤继续:

(pure ()) >>= \_ -> r!x
哈斯克尔怎么知道什么是[
pure()
]

从某种意义上说,它不需要。这里使用
pure()
的函数是
(>>=)
。它有以下类型:

GHCi> :t (>>=)
(>>=) :: Monad m => m a -> (a -> m b) -> m b
m
设置为
可能
,与您的情况一样,我们得到:

Maybe a -> (a -> Maybe b) -> Maybe b
第一个参数的类型是
可能是一个
,因此
(>=)
能够处理任何
可能是一个
值,包括
纯()
,而不管它是一个
只是
-某物还是
。当然,它将以不同的方式处理
只是
什么都不
,因为这就是以下的要点:

我们还需要完成评估。要做到这一点,我们需要知道如何为
Maybe
定义
pure
。我们可以在以下内容中找到定义:

现在我们终于可以继续:

(pure ()) >>= \_ -> r!x
Just () >>= \_ -> r!x
(\_ -> r!x) () -- See the implementation of `(>>=)` above.
r!x

pure()::Maybe()==Just()
?@Mephy
Maybe()==Just()
如何应用于
>=
?没有应用程序,
pure()::Maybe()
被定义为
Just()
,在的第634行。这解决了我的问题,但我在代码中发现了一个新问题,
(x,y,z)@CYC这就是
块的工作方式。
(Just x) >>= k      = k x
Nothing  >>= _      = Nothing
pure = Just
(pure ()) >>= \_ -> r!x
Just () >>= \_ -> r!x
(\_ -> r!x) () -- See the implementation of `(>>=)` above.
r!x