Haskell:模式解析错误:acc
Haskell: Parse error in pattern: acc
我找到了一些云 Haskell 演示,我尝试 运行 它,但我收到一个错误,我不知道为什么。错误看起来像:
MasterSlave.hs:18:9: pattern:acc
中的解析错误
来自 MasterSlave.hs 的代码是:
module MasterSlave where
import Control.Monad
import Control.Distributed.Process
import Control.Distributed.Process.Closure
import PrimeFactors
slave :: (ProcessId, Integer) -> Process ()
slave (pid, n) = send pid (numPrimeFactors n)
remotable ['slave]
-- | Wait for n integers and sum them all up
sumIntegers :: Int -> Process Integer
sumIntegers = go 0
where
go :: Integer -> Int -> Process Integer
go !acc 0 = return acc
go !acc n = do
m <- expect
go (acc + m) (n - 1)
data SpawnStrategy = SpawnSyncWithReconnect
| SpawnSyncNoReconnect
| SpawnAsync
deriving (Show, Read)
master :: Integer -> SpawnStrategy -> [NodeId] -> Process Integer
master n spawnStrategy slaves = do
us <- getSelfPid
-- Distribute 1 .. n amongst the slave processes
spawnLocal $ case spawnStrategy of
SpawnSyncWithReconnect ->
forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
them <- spawn there ($(mkClosure 'slave) (us, m))
reconnect them
SpawnSyncNoReconnect ->
forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
_them <- spawn there ($(mkClosure 'slave) (us, m))
return ()
SpawnAsync ->
forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
spawnAsync there ($(mkClosure 'slave) (us, m))
_ <- expectTimeout 0 :: Process (Maybe DidSpawn)
return ()
-- Wait for the result
sumIntegers (fromIntegral n)
这段代码有什么问题?
您需要启用两个语言扩展,BangPatterns
和 TemplateHaskell
。这些可以通过两种方式启用:
- 编译时从命令行
- 在源文件中使用了扩展名(首选)
要在命令行启用它们,请为您需要的每个扩展传递标志 -XExtensionName
,因此对于您的情况,您需要 ghc -XTemplateHaskell -XBangPatterns source_file_name.hs
.
要在源代码中启用它们,请使用文件顶部的 {-# LANGUAGE ExtensionName #-}
编译指示:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE BangPatterns #-}
module MasterSlave where
...
语言扩展是 GHC 的重要组成部分 Haskell。有一些非常常见,它们出现在大多数现实世界的应用程序中,例如 OverloadedStrings
允许您使用 Text
和 Bytestring
以及 String
文字语法,以及 MultiParamTypeClasses
对于许多更高级的库如 lens
和 mtl
是必不可少的。其他常见的包括 Derive*
扩展,如 DeriveFunctor
、DeriveFoldable
等,它们让编译器派生出比标准 Eq
、Show
、Read
,以及公司
对于您的情况,BangPatterns
添加了用于在函数参数和数据类型字段中指定额外严格性的语法。这有助于减少隐性懒惰带来的问题,但如果您不小心,它也可能会用得太重。 TemplateHaskell
为 GHC 内置的 templating/macro 语言启用了很多额外的语法。库作者可以编写采用数据类型、表达式、函数名称或其他结构的模板 Haskell 函数,并构建不需要留给用户但不容易或不简洁的样板代码否则抽象。 lens
库和 Yesod 网络框架经常使用它。
我找到了一些云 Haskell 演示,我尝试 运行 它,但我收到一个错误,我不知道为什么。错误看起来像:
MasterSlave.hs:18:9: pattern:acc
中的解析错误来自 MasterSlave.hs 的代码是:
module MasterSlave where
import Control.Monad
import Control.Distributed.Process
import Control.Distributed.Process.Closure
import PrimeFactors
slave :: (ProcessId, Integer) -> Process ()
slave (pid, n) = send pid (numPrimeFactors n)
remotable ['slave]
-- | Wait for n integers and sum them all up
sumIntegers :: Int -> Process Integer
sumIntegers = go 0
where
go :: Integer -> Int -> Process Integer
go !acc 0 = return acc
go !acc n = do
m <- expect
go (acc + m) (n - 1)
data SpawnStrategy = SpawnSyncWithReconnect
| SpawnSyncNoReconnect
| SpawnAsync
deriving (Show, Read)
master :: Integer -> SpawnStrategy -> [NodeId] -> Process Integer
master n spawnStrategy slaves = do
us <- getSelfPid
-- Distribute 1 .. n amongst the slave processes
spawnLocal $ case spawnStrategy of
SpawnSyncWithReconnect ->
forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
them <- spawn there ($(mkClosure 'slave) (us, m))
reconnect them
SpawnSyncNoReconnect ->
forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
_them <- spawn there ($(mkClosure 'slave) (us, m))
return ()
SpawnAsync ->
forM_ (zip [1 .. n] (cycle slaves)) $ \(m, there) -> do
spawnAsync there ($(mkClosure 'slave) (us, m))
_ <- expectTimeout 0 :: Process (Maybe DidSpawn)
return ()
-- Wait for the result
sumIntegers (fromIntegral n)
这段代码有什么问题?
您需要启用两个语言扩展,BangPatterns
和 TemplateHaskell
。这些可以通过两种方式启用:
- 编译时从命令行
- 在源文件中使用了扩展名(首选)
要在命令行启用它们,请为您需要的每个扩展传递标志 -XExtensionName
,因此对于您的情况,您需要 ghc -XTemplateHaskell -XBangPatterns source_file_name.hs
.
要在源代码中启用它们,请使用文件顶部的 {-# LANGUAGE ExtensionName #-}
编译指示:
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE BangPatterns #-}
module MasterSlave where
...
语言扩展是 GHC 的重要组成部分 Haskell。有一些非常常见,它们出现在大多数现实世界的应用程序中,例如 OverloadedStrings
允许您使用 Text
和 Bytestring
以及 String
文字语法,以及 MultiParamTypeClasses
对于许多更高级的库如 lens
和 mtl
是必不可少的。其他常见的包括 Derive*
扩展,如 DeriveFunctor
、DeriveFoldable
等,它们让编译器派生出比标准 Eq
、Show
、Read
,以及公司
对于您的情况,BangPatterns
添加了用于在函数参数和数据类型字段中指定额外严格性的语法。这有助于减少隐性懒惰带来的问题,但如果您不小心,它也可能会用得太重。 TemplateHaskell
为 GHC 内置的 templating/macro 语言启用了很多额外的语法。库作者可以编写采用数据类型、表达式、函数名称或其他结构的模板 Haskell 函数,并构建不需要留给用户但不容易或不简洁的样板代码否则抽象。 lens
库和 Yesod 网络框架经常使用它。