haskell - 带警卫的功能:使用“where”时出现语法错误
问题描述
MWE:
import Control.Monad.State.Lazy
fibStep :: State (Integer, Integer) ()
fibStep = state $ \(a, b) -> ((), (b, a + b))
execStateN :: Int -> State s a -> s -> s
execStateN n m s
| n == 1 = execState m s
| n > 1 = let s' = execState m s in
execStateN (n - 1) m s'
-- | n > 1 = execStateN (n - 1) m s' where s' = execState m s
| otherwise = error "undefined behaviour"
它可以工作,但是一旦我取消注释where
变体并对其进行注释let
,它就会给出语法错误:
错误:输入“|”时解析错误</p>
我检查了缩进,它们很好。怎么了?
解决方案
的where
范围是所有守卫,所以你把它放在守卫的末尾,比如:
execStateN :: Int -> State s a -> s -> s
execStateN n m s
| n == 1 = execState m s
| n > 1 = execStateN (n - 1) m s'
| otherwise = error "undefined behaviour"
where s' = execState m s
推荐阅读
- java - ArrayList 在另一个类中访问时为空
- c# - 打开多个邮件时获取发件人电子邮件地址c#
- magento - 打印 Magento 2 的地址/送货标签
- azure-devops - Azure Devops:拉取请求评论:链接任务
- java - 这是什么意思?爪哇
- google-cloud-platform - InfiniteStreamRecognize 实时 STT 示例不起作用
- optimization - 在运行时用移位替换乘以 2
- amazon-web-services - 选择或汇总针对 Cloudwatch 数据代理指标记录的维度
- wso2 - 我需要在 WSO2 Integration Studio 中应用 Aggregator Mediator
- c++ - 类型转换提升多精度整数返回错误答案