gpt4 book ai didi

haskell - 带防护功能 : syntax error when using "where"

转载 作者:行者123 更新时间:2023-12-02 12:17:59 37 4
gpt4 key购买 nike

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 变量,就会出现语法错误:

error: parse error on input ‘|’

我检查了缩进,它们很好。怎么了?

最佳答案

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"
<b>where</b> s' = execState m s

关于haskell - 带防护功能 : syntax error when using "where",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58583869/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com