我正在查看Cloud Haskell软件包的Encoding.hs,并遇到一些奇怪的代码,我希望有人能帮助我更好地理解。包括的是必要的代码:haskell魔法代码,这里发生了什么
class (Binary a,Typeable a) => Serializable a
instance (Binary a,Typeable a) => Serializable a
data Payload = Payload
{
payloadType :: !ByteString,
payloadContent :: !ByteString
} deriving (Typeable)
serialDecodePure :: (Serializable a) => Payload -> Maybe a
serialDecodePure a = (\id ->
let pc = payloadContent a
in pc `seq`
if (decode $! payloadType a) == show (typeOf $ id undefined)
then Just (id $! decode pc)
else Nothing) id
我只是好奇$什么! (我猜只是严格评估),以及为什么我们需要id技巧(有些懒惰评估?)。另外我特别有这个线路问题:
if (decode $! payloadType a) == show (typeOf $ id undefined)
我猜这是看到如果载荷类型是出于某种原因失效,但如果是这样的情况下,不应该再和其他条款进行切换,即改变:
if (decode $! payloadType a) == show (typeOf $ id undefined)
then Just (id $! decode pc)
else Nothing
到
if (decode $! payloadType a) == show (typeOf $ id undefined)
then Nothing
else Just (id $! decode pc)
感谢您的帮助,您可以提供。
是的,'f $! x'是严格的应用程序。你可能会发现[这篇文章](http://neilmitchell.blogspot.com/2008/05/bad-strictness.html)关于如何增加严格性的作品。 –
另请参见http://stackoverflow.com/q/2787543/246886 –